Quantcast
Channel: Question and Answer » raycasting
Viewing all articles
Browse latest Browse all 67

How to refer to a object that has just been hit by a ray cast?

$
0
0

I would like to use RigidbodyConstraints.FreezeRotation;in order to freeze the rotation of an object that is picked up. The problem is I don’t know how to refer (or call) the currently selected object. The script in it’s currents state is below…

public class PhysGun : MonoBehaviour
{
    private GameObject Player;

    void Start ()
    {
       Player = GameObject.Find("Player");
    }

    // Update is called once per frame
    void Update ()
    {
        Vector3 fwd = transform.forward; // Equivalent to the code you had.

        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;

        if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f))
        {
            hit.transform.SetParent(Player.transform);
            Debug.Log("We hit: " + hit.transform.name);
            hit.transform. = RigidbodyConstraints.FreezeRotation;
        }

    }

}

So, how do I call (or refer to) the object that is hit by the ray cast exactly?


Viewing all articles
Browse latest Browse all 67

Latest Images

Trending Articles





Latest Images