Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera smooth change

Discussion in 'Scripting' started by CodeNashor, Nov 29, 2014.

  1. CodeNashor

    CodeNashor

    Joined:
    Jan 6, 2014
    Posts:
    18
    Hello guys!

    I got a ball game and have a own 3rd person camera. It just follows the ball and with 'C', its possible to change the angle of the camera to front,left,right,back. I want to do this change of the camera smoothly. I tried several things, but nothing works out for me :( .

    Vector3.Lerp will be my solution, I guess, like:

    Code (JavaScript):
    1. Vector3.Lerp(Camera.main.transform.position,new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z), 0.5f);
    But i don't know how i should include it to my following script, because it should wait 0.5 seconds, before the normal 'follow the ball' camera should do its job. Maybe you got some input for me, how i could do this.

    Code (JavaScript):
    1.  
    2. if(camera_axis == 1){
    3.             if(camera_x != 0.0){
    4.                 camera_x  = 0.0;
    5.             }
    6.             if(camera_z != -8.0){
    7.                 camera_z  = -8.0;
    8.             }
    9.            
    10.             Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    11.         }
    12.         if(camera_axis == 2){
    13.             if(camera_z != 0.0){
    14.                 camera_z  = 0.0;
    15.             }
    16.             if(camera_x != -8.0){
    17.                 camera_x  = -8.0;
    18.             }
    19.            
    20.             Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    21.         }
    22.         if(camera_axis == 3){
    23.             if(camera_x != 0.0){
    24.                 camera_x  = 0.0;
    25.             }
    26.             if(camera_z != 8.0){
    27.                 camera_z  = 8.0;
    28.             }
    29.            
    30.             Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    31.            
    32.         }
    33.        
    34.         if(camera_axis == 4){
    35.             if(camera_z != 0.0){
    36.                 camera_z  = 0.0;
    37.             }
    38.             if(camera_x != 8.0){
    39.                 camera_x  = 8.0;
    40.             }
    41.            
    42.             Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    43.         }
    44.        
    45.         Camera.main.transform.LookAt(transform);
    46.  
     
  2. CodeNashor

    CodeNashor

    Joined:
    Jan 6, 2014
    Posts:
    18
    I just changed my script a bit, its working pretty stable (of 100 changes - just 1 or 2 which does not get recognized).

    Code (JavaScript):
    1.     function LateUpdate()
    2.     {
    3.         // Camera_axis 1 = normal (foward)
    4.         // Camera_axis 2 = looking to the right (forward = right);
    5.         // Camera_axis 3 = looking to the left (forward = left);
    6.         // Camera_axis 4 = looking back (forward = back);
    7.         if(camera_positioning){
    8.             distance = Vector3.Distance(Camera.main.transform.position, Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z));
    9.             distCovered = (Time.time - startTime) * 0.5;
    10.             fracJourney = distCovered / distance;
    11.             Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z), fracJourney);
    12.             ChillBro();
    13.         }else{
    14.             if(camera_axis == 1){
    15.                 if(camera_x != 0.0){
    16.                     camera_x  = 0.0;
    17.                 }
    18.                 if(camera_z != -8.0){
    19.                     camera_z  = -8.0;
    20.                     camera_positioning = true;
    21.                 }else{
    22.                     Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    23.                 }
    24.             }
    25.             if(camera_axis == 2){
    26.                 if(camera_z != 0.0){
    27.                     camera_z  = 0.0;
    28.                 }
    29.                 if(camera_x != -8.0){
    30.                     camera_x  = -8.0;
    31.                     camera_positioning = true;
    32.                 }else{
    33.                     Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    34.                 }
    35.             }
    36.             if(camera_axis == 3){
    37.                 if(camera_x != 0.0){
    38.                     camera_x  = 0.0;
    39.                 }
    40.                 if(camera_z != 8.0){
    41.                     camera_z  = 8.0;
    42.                     camera_positioning = true;
    43.                 }else{
    44.                     Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    45.                 }
    46.                
    47.             }
    48.            
    49.             if(camera_axis == 4){
    50.                 if(camera_z != 0.0){
    51.                     camera_z  = 0.0;
    52.                 }
    53.                 if(camera_x != 8.0){
    54.                     camera_x  = 8.0;
    55.                     camera_positioning = true;
    56.                 }else{
    57.                     Camera.main.transform.position = new Vector3(transform.position.x+camera_x,transform.position.y+camera_y,transform.position.z+camera_z);
    58.                 }
    59.             }
    60.         }
    61.        
    62.         Camera.main.transform.LookAt(transform);
    63.     }
    64.  
    65. function ChillBro(){
    66.         yield WaitForSeconds (0.05);
    67.         camera_positioning = false;
    68.     }
    69.  
    Hope i can help somebody with the same problem.