I have RayCast represented by a LineRenderer in Unity, so it looks like a laser. I want this laser to move objects it collides with so that the object follows the hit.point
of the Raycast hit.
My code doesn't work, because I move these GameObjects to the hit.point
, which causes the object to comes towards the start point of the Raycast, because a new hit.point
gets calculated since the object is moving to hit.point
. I understand why this is happening, but I'm not sure how to get the object to move with the Raycast, but not effect a new hit.point.
Here's my update function in my script attached to my Laser GameObject. Does anyone know how I can fix my code so that the object moves with the hit.point?
void Update()
{
Vector3 target = calculateDeltaVector();
lr.SetPosition(0, palm.transform.position);
RaycastHit hit;
if (Physics.Raycast(palm.transform.position, target , out hit))
{
if (hit.collider)
{
lr.SetPosition(1, hit.point);
if (hit.transform.gameObject.tag == "Chair")
{
GameObject chair = hit.transform.gameObject;
// !!! move object to hit point, problem HERE
chair.transform.position = hit.point;
hitLock = false;
}
}
}
else lr.SetPosition(1, target * 50);
}