3
votes

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);
    }
2

2 Answers

4
votes

In Unity Inspector, you can select the object and change the layer to "2: Ignore Raycast" This will make the raycast ignore the object and go through it.

0
votes

Don't know, but your code should move the chair to the chair, which likely will make the chair go towards you.

You have to implement Begin and End the raycast move, e.g using mouse click. Below is the example

public class Mover : MonoBehaviour
{
    public Collider selectedChair;

    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" && Input.GetMouseButton(0)) //if you want to move it you have to click mouse button first
                {
                    selectedChair = hit.collider;
                    hit.collider.enabled = false; //disable the collider of currently selected chair so it won't go towards you
                }

                if (selectedChair)
                {
                    // !!! move object to hit point, problem HERE
                    selectedChair.transform.position = hit.point;
                    hitLock = false;
                }

                if (Input.GetMouseButton(0))
                {
                    selectedChair.enabled true;
                    selectedChair = null; //release it
                }
            }

        }
        else lr.SetPosition(1, target * 50);
    }
}