3

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);
    }
karamazovbros
  • 950
  • 1
  • 11
  • 40

2 Answers2

4

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.

TechnoRazor
  • 128
  • 1
  • 8
  • I want the object to be detectable by the hit, because this laser moves objects. For instance, if you hit a "chair" object then the laser would move the chair around by the point it got hit until the laser is turned off. – karamazovbros Aug 30 '18 at 22:23
  • So would the chair be a fixed distance from the laser's origin or would the chair move back and forth depending on where you aim? – TechnoRazor Aug 30 '18 at 22:30
  • The chair (as well as other objects) could be at any distance away. Say it got hit with this "tractor beam" of sorts, then the laser can move it anywhere in the range it got hit. – karamazovbros Aug 30 '18 at 22:35
  • What I mean is, let's say the chair was 10 units away from the origin when it got hit. If you moved the target to point somewhere that is 15 units away, then would the chair also go 15 units away or stay at 10 units away? – TechnoRazor Aug 30 '18 at 23:31
  • It would stay at 10 units, the laser Raycast would also be stopped at the point it hits the object, so it wouldn't be able to keep going through the object. – karamazovbros Aug 30 '18 at 23:34
  • Could you explain what the line `chair.transform.parent = hit.transform;` is supposed to do? – TechnoRazor Aug 30 '18 at 23:51
  • Whoops, for this question it should be chair.transform.position. I was trying to move it with the transform of another object. I'm sorry, I'll change that to chair.transform.position = hit.point. – karamazovbros Aug 30 '18 at 23:57
0

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);
    }
}
endrik exe
  • 606
  • 3
  • 10