- Home /
 
Getting Camera to Stop Following Player
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.
TL;DR The problem is that the camera moves it's position back on it's x axis, instead of just staying where it is.
Any ideas?
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;
          }
      }
 
               Thanks!
Answer by hexagonius · Jan 06, 2015 at 09:55 AM
Try this:
 public float addPos;
 public Transform target;
 
 void LateUpdate () {
   if(!Astronaut.isDead){
     transform.position = target.transform.position + (transform.position - target.transform.position).normalized * addPos;
   }
 }
 
              Your answer
 
             Follow this Question
Related Questions
how to stop the camera to follow the player on his jump movement ? 2 Answers
Camera rotation around player while following. 6 Answers
Match position of two object without making one a child of the other 4 Answers
how can do click then move on a gameobject? 1 Answer
follow along the XZ plane? 0 Answers