So I'm trying to raycast on mouse click if it hits any thing make that object a child of the sender (in this case the parent). Below is my code...

public class PhysGun : MonoBehaviour
    private GameObject Player;

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

    // Update is called once per frame
    void Update ()
        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        if (Input.GetMouseButton(0) && Physics.Raycast(transform.position, fwd, 10))
            //this.transform.parent = Player.transform;
            Debug.Log("There is something in front of the object!");


The script is attached to a child of the player and I will eventually attatch it to a motion control (Vive).

What am I doing wrong exactly? I've tried multiple ways to do this and none of them work (or present any errors). Also to clarify the issue it's not parenting the game object hit by the ray cast to the Player like I want it to.

  • \$\begingroup\$ In what way is this not working? What symptom are you trying to fix? \$\endgroup\$ – DMGregory Apr 27 '16 at 0:23
  • \$\begingroup\$ It's not parenting the game object hit by the ray cast to the Player like I want it to. \$\endgroup\$ – Sie Apr 27 '16 at 0:29

When you write transform without anything in front of it, it refers to this.transform, ie. the transform attached to the GameObject this script instance is running on.

It sounds like you instead want to modify the transform of the object hit by the raycast. To do this, we need to capture a reference to it...

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

if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f))
    Debug.Log("We hit: " + hit.transform.name);

That out hit parameter tells the Raycast function to capture information about what it struck (like the object, contact point & normal, distance...), so you can access it through the hit variable. hit.transform gives us the transform of the struck object.

| improve this answer | |
  • \$\begingroup\$ I tried that but I had a couple of errors. I'll see what I can do to get it working. \$\endgroup\$ – Sie Apr 27 '16 at 0:58
  • \$\begingroup\$ Sorry, I got the arguments in the wrong order. Fixing now. Should be Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity). When in doubt, consult the docs \$\endgroup\$ – DMGregory Apr 27 '16 at 1:01
  • \$\begingroup\$ Much better. However I don't know why but the hit keeps on going through the object I want to pick up. Here is a quick video to show that problem. I will add a tag check to make sure but it shouldn't be going right through the object right? \$\endgroup\$ – Sie Apr 27 '16 at 1:06
  • \$\begingroup\$ Depends. Does it have a 3D collider attached to it? Is it fully in front of the ray? There are a lot of things that can cause a raycast to not hit an object, so you'd need to show more details of your scene setup to diagnose what's happening. \$\endgroup\$ – DMGregory Apr 27 '16 at 1:07
  • \$\begingroup\$ Well I think the object was just a wee bit too small. I got it working. Thanks! \$\endgroup\$ – Sie Apr 27 '16 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.