How do you access the transform of a gameobject and put it in a variable?
Okay so this is a script for the movement of the camera. And I want it to follow the cube called "Player". I know I need to access the transform of "Player" but how. So how can I make a variable called "player" that can access the transform of the cube, so I can put it in as player.position?All I need is the variable. Help please!
public float rotationSpeed = 20f;
if (Input.GetKey (KeyCode.R)) {
transform.RotateAround(player.position, rotationSpeed * Time.deltaTime);
}
Answer by Dibbie · Nov 07, 2015 at 01:12 AM
public Vector3 playerPosition;
public Transform player;
void Update(){
playerPosition = player.position;
}
Though doing it in this method seems a bit... Inefficient, you can get your players GameObject, and just have the camera linked to the player in the hierarchy, to actually have it follow the player, then by script just do a LookAt with the camera to the referenced player.
public GameObject player;
void Update(){
camera.main.transform.LookAt(player.transform);
}
Your answer
Follow this Question
Related Questions
Help with Obstacle. Please Help. 1 Answer
Vector3.Lerp not working properly, making the player bounce around 2 Answers
Transform.position of object not the same as shown on the inspector 1 Answer
Question about Vector creation 1 Answer
How To Make An Object Appear In Front Of Player Without Cloning? 1 Answer