'Do a Raycast after Raycast-positioned object
I have a code that allows to position an object onto the other object by Raycast. Obviously, I am using Mesh Collider so everything works fine.
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.GetComponent<SelectableTrunk>())
{
RandomSTrunk.position = hit.point;
}
}
My question is the next. Is it possible to place the object (as I did) and then do one more raycasting to place the other object onto the already placed one?
When I am trying to do that - The mesh collider on the first object is breaking everything and nothing works.
Solution 1:[1]
Specify a layer for the hit object and then follow the code below:
public LayerMask hitPlaceLayer;
void RayCast()
{
// first ray cast
if (Physics.Raycast(transform.position, transform.forward, out var hit, hitPlaceLayer.value))
{
RandomSTrunk.position = hit.point;
}
// second raycast
if (Physics.Raycast(transform.position, transform.forward, out hit, hitPlaceLayer.value))
{
otherRandomTrunk.position = hit.point;
}
}
define layer for Hit place object:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |