Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to rotate smoothly behind target?

Discussion in 'Scripting' started by JonasJSK, Jan 20, 2014.

  1. JonasJSK

    JonasJSK

    Joined:
    Apr 6, 2013
    Posts:
    2
    Hello!

    Here is my code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class CameraController : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public Texture crosshairTexture;
    8.     public float sensivitiy = 3.3f;
    9.     public float zoomSpeed = 35;
    10.     public float maxRotateBehind = 130;
    11.  
    12.     private float x, y;
    13.     private float distance = 4;
    14.     private float currentDistance, correctedDistance;
    15.     private bool isRotatingBehind = false;
    16.     private Vector3 targetOffset;
    17.     private RaycastHit hit;
    18.     private LayerMask layerMask;
    19.     private Rect crosshairPosition;
    20.  
    21.     void Awake()
    22.     {
    23.         targetOffset = new Vector3(0, -target.localScale.y + .75f, 0);
    24.         correctedDistance = currentDistance = distance;
    25.  
    26.         crosshairPosition = new Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) / 2, crosshairTexture.width, crosshairTexture.height);
    27.  
    28.         Screen.showCursor = false;
    29.     }
    30.  
    31.     void LateUpdate()
    32.     {
    33.         //Rotation
    34.         if (Input.GetButton("LookBehind"))
    35.             x += Input.GetAxis("Mouse X") * sensivitiy;
    36.         else
    37.             x = target.eulerAngles.y;
    38.  
    39.         y -= Input.GetAxis("Mouse Y") * sensivitiy;
    40.  
    41.         y = Mathf.Clamp(y, -45, 75);
    42.         x = Mathf.Clamp(x, target.localEulerAngles.y - maxRotateBehind, target.localEulerAngles.y + maxRotateBehind);
    43.  
    44.         Quaternion rotation = Quaternion.Euler(y, x, 0);
    45.  
    46.         //Position
    47.         Vector3 position = target.position - (rotation * Vector3.forward * distance + targetOffset);
    48.  
    49.         //Collision
    50.         if (Physics.Linecast(target.position, position, out hit))
    51.             correctedDistance = Vector3.Distance(target.position, hit.point) - .5f;
    52.         else
    53.             correctedDistance = Mathf.Lerp(currentDistance, distance, zoomSpeed * Time.deltaTime);
    54.  
    55.         currentDistance = Mathf.Lerp(correctedDistance, currentDistance, zoomSpeed * Time.deltaTime);
    56.         position = target.transform.position - (rotation * Vector3.forward * currentDistance + targetOffset);
    57.  
    58.  
    59.         transform.rotation = rotation;
    60.         transform.position = position;
    61.     }
    62.  
    63.     void OnGUI()
    64.     {
    65.         GUI.DrawTexture(crosshairPosition, crosshairTexture);
    66.     }
    67. }
    68.  
    When i press my LookBehind button i can rotate the camera around the player, so i can look behind him. But when i release the button, i want the camera to smooth back behind the player. I can't figure out how to do it, any help?

    If it helps i am trying to create a third person camera like in DayZ.
     
    Last edited: Jan 21, 2014
  2. JonasJSK

    JonasJSK

    Joined:
    Apr 6, 2013
    Posts:
    2
    Bump :>
     
  3. TheShane

    TheShane

    Joined:
    May 26, 2013
    Posts:
    136
    It's hard to look at the code and run it your mind so you might get more help if you were more specific about what isn't working (preferably with a video of its current behaviour). In general, I recommend using the Quaternion API when you're dealing with rotations instead of messing with euler angles. You can deal with rotations around an arbitrary axis in a more straightforward way than hacking at the euler angles.

    I would also suggest taking a look at the new sample assets Unity released for free on the asset store. They have a few camera look scripts that work, and that you should be able to tweak to get the exact behaviour you want.