Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Camera lateupdate movement

Discussion in 'Scripting' started by xBonic, Jul 14, 2018.

  1. xBonic

    xBonic

    Joined:
    Mar 27, 2017
    Posts:
    1
    Hi all,

    I have tried to do camera movement like this game has

    but I cannot get anything quite close to it.
    Please help if anyone has ideas on how to do it thanks!

    Code (CSharp):
    1. public class BaseCamera : MonoBehaviour
    2. {
    3.  
    4.  
    5.         // Default FOV
    6.         protected float     m_DefaultFOV                    = 60;
    7.  
    8.         // Camera component
    9.         protected Camera    m_Camera                        = null;
    10.  
    11.  
    12.         // Camera component accessors
    13.         public Camera CameraComponent
    14.         {
    15.             get { return m_Camera; }
    16.         }
    17.  
    18.  
    19.         // Use this for initialization
    20.         protected void Start()
    21.         {
    22.             // Get camera component
    23.             m_Camera = GetComponent<Camera>();
    24.  
    25.             // Get default FOV
    26.             m_DefaultFOV = m_Camera.fieldOfView;
    27.         }
    28.  
    29. }
    30.  
    31. public class TPSCamera : BaseCamera
    32. {
    33.                              // Character
    34.         public GameObject   m_Character                     = null;
    35.  
    36.         // Offset
    37.         [SerializeField]
    38.         private Vector3     m_CharacterOffset               = new Vector3(0, 1, -6);
    39.  
    40.         // Character up before dodge
    41.         private Vector3     m_CharacterUpBeforeDodge        = Vector3.zero;
    42.  
    43.         // Lerp factor
    44.         [SerializeField]
    45.         private float       m_LerpFactor                    = 6;
    46.  
    47.   [SerializeField]
    48.   private bool Customize = true;
    49.   [SerializeField]
    50.   private float turnSpeed = 4.0f;
    51.  
    52.   private new void Start()
    53.   {
    54.     if (Customize)
    55.     {
    56.       m_CharacterOffset.x = 5;
    57.       m_CharacterOffset.z = -8;
    58.       this.gameObject.transform.LookAt(m_Character.transform.position);
    59.     }
    60.   }
    61.   void FixedUpdate()
    62.   {
    63.     //  Camera follow character. Written in fixed update to avoid camera lerp break
    64.     if (m_Character != null)
    65.     {
    66.       // Calculate local offset depending on dodge action
    67.       Vector3 localOffset = m_Character.transform.right * m_CharacterOffset.x + m_Character.transform.up * m_CharacterOffset.y + m_Character.transform.forward * m_CharacterOffset.z;
    68.  
    69.       // Update position based on offset
    70.       Vector3 desiredPosition = m_Character.transform.position + localOffset;
    71.       transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.fixedDeltaTime * m_LerpFactor);
    72.     }
    73.  
    74.   }
    75.  
    76.   private void Update()
    77.   {
    78.  
    79.   }
    80.  
    81.   void LateUpdate()
    82.   {
    83.     if (Customize)
    84.     {
    85.       if (Input.GetAxis("Mouse X") != 0 && Input.GetMouseButton(0))
    86.       {
    87.         m_CharacterOffset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * m_CharacterOffset;
    88.       }
    89.       transform.position = m_Character.transform.position + m_CharacterOffset;
    90.       transform.LookAt(m_Character.transform.position);
    91.     }
    92.   }
    93. }
    94.  
    95. public class PlayerController : MonoBehaviour
    96. {
    97.     public Camera m_Camera;
    98.     public float speed;
    99.     public float tilt;
    100.     public Boundary boundary;
    101.     public float speedH = 2.0f;
    102.     public float speedV = 2.0f;
    103.     private float yaw = 0.0f;
    104.     private float pitch = 0.0f;
    105.     public Rigidbody rb;
    106.     void Start()
    107.     {
    108.         // m_MouseLook.Init(transform, m_Camera.transform);
    109.         rb = GetComponent<Rigidbody>();
    110.     }
    111.     void Update()
    112.     {
    113.         if (health < 1)
    114.         {
    115.             Time.timeScale = 0;
    116.             gameoverText.GetComponent<Text>().enabled = true;
    117.         }
    118.            
    119.         float moveHorizontal = Input.GetAxis("Horizontal");
    120.         float moveVertical = Input.GetAxis("Vertical");
    121.         yaw += speedH * Input.GetAxis("Mouse X");
    122.         pitch -= speedV * Input.GetAxis("Mouse Y");
    123.         transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    124.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    125.         if (moveVertical != 0)
    126.         {
    127.             // transform.position += transform.forward * speed * moveVertical * Time.deltaTime;
    128.             rb.velocity += transform.forward * speed * Time.deltaTime;
    129.         }
    130.         else
    131.         {
    132.             rb.velocity = Vector3.zero;
    133.         }
    134.         //GetComponent<Rigidbody>().velocity = movement * speed;
    135.         GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().position.x * -tilt);
    136.     }
    137. }
    138.  
     
    Last edited: Jul 14, 2018