How does this part of the script make sense?
Sorry I'm new to programming. Please forgive me if this question is stupid.
I found this in the 2D UFO Tutorial.
using UnityEngine; using System.Collections;
public class CompleteCameraController : MonoBehaviour {
public GameObject player; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start ()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - player.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate ()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = player.transform.position + offset;
}
}
Ok if, offset = transform.position - player.transform.position; AND transform.position = player.transform.position + offset;
which means transform.position = player.transform.position + transform.position - player.transform.position;
simplify it we got player.transform.position=player.transform.position;
Yes.. Equation is correct, but what??
Answer by Eno-Khaon · Mar 30, 2016 at 10:19 AM
Let's take a visual approach to this:
First, you have your character and camera and Vector2(5, -3) and Vector2(16, 2) respectively.
The difference between those two points is a subtraction of one from the other.
A ----------------> B
To get the Vector going from point A to point B, you subtract A from B. In other words, AtoB = B - A
.
In the context of the drawing, that means your camera's offset from the player is (16, 2) - (5, -3)
, resulting in a difference of (11, 5)
. So, at all times, this camera should remain 11 units to the right of, and 5 units above the player.
The Start() function in Unity runs once per object, when it first enters the game. This gives the opportunity to generate that offset based on the player's and camera's starting positions without being influenced by their locations later.
The LateUpdate() function, much like the Update() function, takes place once per frame of gameplay. Because of this, your player's position will likely be changing rapidly. However, you want the camera to remain that same distance away from the player at all times. That's why we determine what its offset should be in the Start() function.
All in all, it's actually nothing more than a simple oversight.
transform.position = player.transform.position + transform.position - player.transform.position;
is not "wrong", but it's not right either. What's actually happening is more like this:
transform.position = player.transform.position + (transform.originalPosition + player.transform.originalPosition);
It would be silly and ineffective to recalculate the camera's offset on every frame of gameplay. Likewise, because it's NOT updating every frame, it is a constant value to make use of instead.
I hope this answered your question adequately. If not, there's always room for more doodles!
Oh I got this now. So in Void Start() the offset is calculated, but the position in the Void Start() won't be updated. So actually the positions in Void Start() and Void LateUpdate() are different. Thanks!
And may I ask why use LateUpdate() ? The tutor in that video said because LateUpdate() make sure to execute after Update(), but there is nothing in the Update() is there? So why do we need LateUpdate() anyway?
LateUpdate() is used as something of a safety precaution.
Every script on every object in your scene will run through their Update() function. Then, after every object has done so, they'll move on to LateUpdate().
http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
The idea behind putting the camera positioning into LateUpdate() is that you would handle most non-physics-engine character movements and adjustments in Update(), so you're making sure that everything is done and in place and ready before telling the camera what to look at, without luck beco$$anonymous$$g a factor.
If the objects happen to run through their Update() functions in the wrong order for what you need, you could wind up with your camera looking at an empty space because the object you wanted it to see just happened to move to the side at the wrong time.
Race conditions suck, though, so LateUpdate() is there to let you choose what need to happen last.
Answer by Doireth · Mar 30, 2016 at 09:09 AM
When you take one vector from another what you get is a directional vector from that vector to the other. For example
offset = transform.position - player.transform.position;
Means that "offset" is a directional vector from "player.transform.position" to "transform.position". Keep in the mind that this is a direction and can be used anywhere in space (not dependent on starting point).
So, by using the code:
transform.position = player.transform.position + offset;
You are taking the player's position and adding that direction (offset variable) to get the new position for the transform (transform.position).
You cannot "simplify" the code the way that you tried. It doesn't work that way.
Answer by giorashc · Mar 30, 2016 at 09:45 AM
When offset is first calculated in Start()
the player is in a certain position. In the LateUpdate()
method the player might already moved so its position is different then the one used in the Start()
method so player.transform.position
in the LateUpdate()
method is not the same as it was in the Start()
method.
The offset is used to move the camera so it will always be at the same distance and direction from the player.
(if the player does not move at all then you are right but most likely the player will move and change its position in the game)
Your answer
Follow this Question
Related Questions
Move the objects Y? 0 Answers
How to make my snowball model to actually shoot like a projectile? 0 Answers
Are nested ifs different from &&? 1 Answer
Have Two variables in a if statment? 1 Answer
How to interact with closed doors 0 Answers