- Home /
Regarding transform.position in the roll a ball tutorial
So in the tutorial we had a rolling ball, and were supposed to attach the camera to it. Doing it via the hierarchy menu made it mirror every movement, so it spun around, so we had to write a script, the script looks as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camer : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
}
}
Now heres my rather noob question: Why or How exactly does this work? Its not really explained in the tutorial or I did not really get it :( In my understanding, we define a vector called offset that is supposed to mark the camera position, then we define it to be the transform.position, which I assume is just the initial position of the camera, minus the player.transform.position, which is the position of the ball. Then we update out camera transform.position with another player.transform.position + offset
Now in my understanding this shouldnt do anything? Since we just got the initial transform.position of the camera, then subtracted the player position, and then added it again? In my mind this would work out to be a camera that should not move at all, but it is moving focused on the ball. Can anyone help me with understanding this better?
Answer by PlayCreatively · Feb 23, 2018 at 05:10 PM
Well the offset is only assigned ones and never changes.
You mean its only assigned once? This is what confuses me: If I interpret it right, the following has happened: First we called for the player object and defined a vector, the vector has been defined as the difference between the cameras position (which we have, since the script is running on the camera), and the players position. This is defined only once for the Start, since we are using Start(). So we now have the offset vector. Then we define the cameras position to be the players current position, since we use LateUpdate() + our defined offset vector, making the camera follow around the player. Everything here makes sense if you are not confused like me :P
What I didnt understand is this: This script was essentially a "fix" for the issue that the camera would mimic the players movements, and since in the game we had a ball rolling, the camera would spin with its assigned "position" on the ball. So previously I assumed that the player.transform.position command would include this spinning, which is evidently just not the case, it really is just the position coordinates of the object.?
Thanks for your answer!
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Independent Camera movement,Camera movement 1 Answer
Disable Camera Movement,Stop camera movement 0 Answers
Multiple Cars not working 1 Answer