- Home /
 
Attaching Main Camera to a GameObject
I am trying to create a function (in javascript) where if you press a button (R), the main camera will become a child of the game object (the vehicle).
 #pragma strict
 
 public var moveSpeed : float = 10f;
 public var turnSpeed : float = 50f;
 
 
 function Update ()
 {
 
     if(Input.GetButtonDown("R"))
         //main camera to game object here
     
     if(Input.GetKey(KeyCode.UpArrow))
         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.DownArrow))
         transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.LeftArrow))
         transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
     
     if(Input.GetKey(KeyCode.RightArrow))
         transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
 }
 
              How could I make the main camera the child of a game object when I press 'R' using javascript?
Answer by Natsu_k · Jan 15, 2015 at 03:13 AM
Since camera is just a component, a better way to ask would be how do I attach a component to a gameObject from another object; to which your answer is found here:
Your answer
 
             Follow this Question
Related Questions
Flight Script throws no errors but does nothing. 2 Answers
How do I spawn a prefab to be the child of a mesh? 1 Answer
need help translating camera control script from C# to javascript 1 Answer
Access parents other child from other other child 1 Answer
Calling a function via button from instance of prefab 1 Answer