- Home /
Question by
slader155 · Jan 06, 2015 at 11:19 AM ·
cameragameobjecttransformpositionfollow player
Following a gameObject, unexpected problems?
Hi Everyone,
I am trying to make my camera follow the main character until he collides with a gameObject. So far everything works as expected, the camera follows the character, and I have a boolean that detects if the character collides with an object. But the camera doesn't stop following the player when he hits something.
Here is my script:
public float addPos;
public Transform target;
private Vector3 targetPos;
void Update(){
targetPos = new Vector3(target.position.x + addPos, transform.position.y, transform.position.z);
}
void LateUpdate () {
if(gameObject.name == "Main Camera"){
if(!Astronaut.isDead){
transform.position = targetPos;
}else{
transform.position = transform.position;
}
}
Comment
Answer by braco86 · Jan 06, 2015 at 12:13 PM
Hi, LateUpdate() is called after all Update() Functions. That means, after Update(), LateUpdate() is called and then Update is called AGAIN and so on. Try to check the Death of your Astronaut i your Update() Function.
public float addPos;
public Transform target;
private Vector3 targetPos;
void Update(){
if (!Astronaut.isDead) {
targetPos = new Vector3(target.position.x+addPos,transform.position.y,transform.position.z);
}
}
void LateUpdate () {
if(gameObject.name == "Main Camera"){
if(!Astronaut.isDead){
transform.position = targetPos;
}else{
transform.position = transform.position;
}
}