- Home /
Leaning/peeking
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);
}
}
You could make a public transform for each point you want to lean to and lerp it between current position and the others.
Answer by CoreDLL · May 22 at 05:01 PM
Make your camera gameObject a child of the pivot gameObject, then move the camera up in the local space relative to the pivot point. Now if you rotate the pivot the camera will keep the offset and will rotate like an arch.