Search Unity

Interpolation for "Switched" Camera movement C#

Discussion in 'Scripting' started by ddc_Cl, Jul 4, 2015.

  1. ddc_Cl

    ddc_Cl

    Joined:
    Jul 4, 2015
    Posts:
    4
    Heyo i need help for my game i wanna make a camera movement similar to Atari's Ballance
    Cuz i am making a similar game as a homenaje and i need this for finnish the Basic scripting and start with the level design

    I aready have a code that Make the camera Switch to 4 position but is like the camera teleport and i wanna make that Move to the next position

    Here is the Code

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public enum CameraPivot
    6. {
    7.     Forward,
    8.     Right,
    9.     Left,
    10.     Back
    11. }
    12.  
    13. public class CameraFollow : MonoBehaviour
    14. {
    15.     public GameObject target;
    16. //    public float GradosGiro = 25;
    17.     public float distToTarget = 2.3f;
    18.     public float height = 2.1f;
    19.  
    20.     public CameraPivot cameraPivot = CameraPivot.Back;
    21.     void Start ()
    22.     {
    23.         height = 2.1f;
    24.         distToTarget = 4;
    25.  
    26.         }
    27.    
    28.     void Update()
    29.     {
    30.         if (target == null)
    31.             return;
    32.  
    33.         UpdateCameraInput ();
    34.  
    35.         Vector3 pivot = GetPivot();
    36.         Vector3 newPos = (target.transform.position + pivot*distToTarget) + new Vector3(0,height,0);
    37.         transform.position = newPos;
    38.  
    39.         transform.LookAt (target.transform);
    40.  
    41.     }
    42.  
    43.     void UpdateCameraInput() // this Void make the Camera Change position
    44.     {
    45.         bool left = Input.GetKeyDown (KeyCode.Q);
    46.         bool right = Input.GetKeyDown (KeyCode.E);
    47.         if (right)
    48.         {
    49.             switch(cameraPivot)
    50.             {
    51.             case CameraPivot.Back:
    52.                 cameraPivot = CameraPivot.Right;
    53.                 break;
    54.             case CameraPivot.Right:
    55.                 cameraPivot = CameraPivot.Forward;
    56.                 break;
    57.             case CameraPivot.Forward:
    58.                 cameraPivot = CameraPivot.Left;
    59.                 break;
    60.             case CameraPivot.Left:
    61.                 cameraPivot = CameraPivot.Back;
    62.                 break;
    63.             }
    64.         }
    65.         else if (left)
    66.         {
    67.             switch (cameraPivot)
    68.              {
    69.                 case CameraPivot.Back:
    70.                 cameraPivot = CameraPivot.Left;
    71.                 break;
    72.                 case CameraPivot.Left:
    73.                 cameraPivot = CameraPivot.Forward;
    74.                 break;
    75.                 case CameraPivot.Forward:
    76.                 cameraPivot = CameraPivot.Right;
    77.                 break;
    78.                 case CameraPivot.Right:
    79.                 cameraPivot = CameraPivot.Back;
    80.                 break;
    81.             }
    82.            
    83.         }
    84.     }
    85.     Vector3 GetPivot()
    86.     {
    87.         switch (cameraPivot)
    88.         {
    89.         case CameraPivot.Back:
    90.             return -Vector3.forward;
    91.         case CameraPivot.Forward:
    92.             return Vector3.forward;
    93.         case CameraPivot.Right:
    94.             return Vector3.right;
    95.         case CameraPivot.Left:
    96.             return -Vector3.right;
    97.         }
    98.  
    99.         return Vector3.zero;
    100.     }
    101. }
    102.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Instead of assigning the camera position, all the calculations you're currently doing should instead set a "target" property (of type Vector3).

    Then, on every frame, use Vector3.MoveTowards to move the camera smoothly towards the target.
     
  3. ddc_Cl

    ddc_Cl

    Joined:
    Jul 4, 2015
    Posts:
    4
    The Camera Follow the target, what i want its a smoth move when the camera change position
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    By "target" I mean "where the camera should be." Read my previous post with that understanding, and I think you'll get it.