Hello, recently i’ve been trying to implement Leaning into my fps game but it doesn’t work like i wanted to. My camera is tilting left and right but its just rotation, i want it to move a little to the left/right so i can peek corners. Can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeanBehaviour : MonoBehaviour
{
public Transform _Pivot;
public float speed = 100f;
public float maxAngle = 20f;
float curAngle = 0f;
// Use this for initialization
void Awake()
{
if (_Pivot == null && transform.parent != null) _Pivot = transform.parent;
}
// Update is called once per frame
void Update()
{
// lean left
if (Input.GetKey(KeyCode.Q))
{
curAngle = Mathf.MoveTowardsAngle(curAngle, maxAngle, speed * Time.deltaTime);
}
// lean right
else if (Input.GetKey(KeyCode.E))
{
curAngle = Mathf.MoveTowardsAngle(curAngle, -maxAngle, speed * Time.deltaTime);
}
// reset lean
else
{
curAngle = Mathf.MoveTowardsAngle(curAngle, 0f, speed * Time.deltaTime);
}
_Pivot.transform.localRotation = Quaternion.AngleAxis(curAngle, Vector3.forward);
}
}