- Home /
 
Camera Controller movement issue
Hello, I'm using unity to make a game on rails, and I wanted the camera to have a little bit of lag when my character moves to the left and right. I implemented a pretty simple one (camera only moves so fast towards where the character is).
However I found that it moves differently depending on if the character is moving to the left or the right. So if the Character moves to the right it moves how I expect. But it moves much faster if he is moving to the right, and if I have the camera updating slowly enough it lists to the left. I can't figure out why.
Any help would be great. Alternatively if anyone has any ideas/links as to how else I could implement the camera that would be appreciated. The code for the main camera controller is below.
 #pragma strict
 // player and camera
 var Play : GameObject;
 var MainCamera : GameObject;
 //Start position of camera, used for zero error  
 var startPosz : float;
 var startPosx : float;
 var startPosy : float;
 //Current position of camera
 var curPosz : float;
 var curPosy: float;
 var curPosx: float;
 //Position that the camera is moving to 
 var desiredPosz : float;
 var desiredPosy: float;
 var desiredPosx: float;
 //Speed that the camera follows the player
 var CameraSpeed: float;
 
 
 
 function Start () {
     Play = GameObject.FindGameObjectWithTag("Player");
     MainCamera = GameObject.FindGameObjectWithTag("MainCamera");
     // issue if the camera is slower than 0.8 (listing to the left)
     CameraSpeed = 0.1;
     startPosz = Play.transform.position.z;
     startPosy = Play.transform.position.y;
     startPosx = Play.transform.position.x;
     
 }
 
 function Update () {
     // desired location of the camera, z is moving forward, y is up and down, x is side to side 
     desiredPosz = Play.transform.position.z;
     desiredPosy = Play.transform.position.y;
     desiredPosx = Play.transform.position.x;
     //only x has lag at the moment  
     curPosx = MainCamera.transform.position.x;
     
     //update camera for forward movement
     if (desiredPosz != startPosz){
         transform.Translate(0.0f, 0.0f, (desiredPosz - startPosz));
         startPosz = desiredPosz;
     }
     
     //update camera for up/down (jumping, not tested should work) 
     if (desiredPosy != startPosy){
     //                  Horizontal,                  Verticle, Depth 
         transform.Translate(0.0f, (desiredPosy - startPosy), 0.0f);
         startPosy = desiredPosy;
     }    
     
     //Basic version follows pecisley 
     /*if (desiredPosx != startPosx){
     //                    Horizontal, Verticle, Depth 
         transform.Translate((desiredPosx - startPosx),0.0f, 0.0f);
         startPosx = desiredPosx;
     }*/
     
     if (desiredPosx != startPosx){
     
         if(curPosx + CameraSpeed < desiredPosx){
             curPosx = curPosx + CameraSpeed;
             
         } else if(curPosx - CameraSpeed > desiredPosx){
             curPosx = curPosx - CameraSpeed;
             
         }else{
             curPosx = desiredPosx;
             
         }
         transform.Translate((curPosx - startPosx ), 0.0f, 0.0f);
         startPosx = curPosx;
     }
 }
 
              Your answer