Search Unity

Making Cinemachine tilt upwards/downwards when walking on a slope.

Discussion in 'Cinemachine' started by der_stein, Nov 27, 2018.

  1. der_stein

    der_stein

    Joined:
    Nov 27, 2018
    Posts:
    1
    So I have a camera that follows the player character and I want it to tilt slightly up/down when walking on slopes. As to give the player a better overview when moving around. I have tried making a CinemachineCore.AxisInputDelegate to give this effect. However, my code makes the camera shake weirdly up and down even on an even surface. It sometimes produces the desired effect when walking up/down but it seems random. I am new to using Cinemachine so I don't know if I have just misunderstood how to use this or what. I hope someone is able to help me. Here is my code:

    Code (CSharp):
    1. public class CamTilter : MonoBehaviour {
    2.  
    3.     [SerializeField]
    4.     private Transform startPoint;
    5.     [SerializeField, Tooltip("How much slope is needed before cam tilting sets in (in degrees)")]
    6.     private float deviation = 10f;
    7.     private RaycastHit hit;
    8.     private const float RIGHTANGLEINDEGREES = 90f;
    9.     private float tiltMultiplier = 0.1f;
    10.  
    11.     // Use this for initialization
    12.     void Start() {
    13.         CinemachineCore.GetInputAxis = InputDelegate;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update() {
    18.         Physics.Raycast(startPoint.position, Vector3.down, out hit);
    19.     }
    20.  
    21.     float InputDelegate(string axisName) {
    22.         if(axisName.Equals("Mouse Y")) {
    23.             float angle = Vector3.SignedAngle(startPoint.position, hit.normal, Vector3.up);
    24.             angle = Mathf.Abs(angle);
    25.             if(Mathf.Abs(RIGHTANGLEINDEGREES - angle) < deviation) {
    26.                 return 0f;
    27.             }
    28.  
    29.             return (RIGHTANGLEINDEGREES - angle) * tiltMultiplier;
    30.         }
    31.         return 0f;
    32.     }
    33.  
    34. }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    If you want the camera to tilt then modifying the mouse Y input doesn't seem to be the right way to do it. Instead, try implementing a CinemachineExtension that adds a tilt to the camera rotation as an orientation correction