Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Parent an object to the camera via script?

$
0
0
I have a script so far and I want it to send a raycast out to an object and then attach that object to the main camera - like in Skyrim and Portal etc. I know I will have to do something else to make it have a rigid body but for now I want it to parent it to the camera as long as the E key is pressed and then un-parent it when it is let go. My script so far: #pragma strict function Update () { if (Input.GetKey ("e")) { var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5, 0.5, 0)); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { //hit.transform.SendMessage ("PickUp", SendMessageOptions.DontRequireReceiver); } } } I put // in there to show that that's how I think it may work but if you know a better way then you can say. So, how can I go about doing this. I guess somehow make the raycast send a message to the object it hits and then the object will run a function where it attaches itself to the camera?

Viewing all articles
Browse latest Browse all 171066

Trending Articles