I currently implemented so that my Character leans in the direction he is walking on Horizontal Layer. So if he presses ‘D’ he leans to the right and ‘A’ he leans to the left.
I got that working, but: If i am in the state of leaning in on a specific side, but press the Camera other side its snapping to 0 and then moves on leaning to the other side.
I can imagine using some Quaternion.Lerp or some Mathf. for smoothing out rotation but i don’t really know how.
Here’s the code:
private void HandleMouseLook()
{
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * 5 * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * 5 * Time.deltaTime;
Vector2 targetMouseDelta = new Vector2(mouseX, mouseY);
float tilt = -Input.GetAxis("Horizontal") * tiltAmount; // TILTING
// SMOOTHING AND ACCELERATION
if (mouseSmoothing == true)
{
Vector3 v = transform.rotation.eulerAngles;
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, smoothTime, acceleration);
rotationX -= currentMouseDelta.y; // OBEN UND UNTEN ROTATION
rotationX = Mathf.Clamp(rotationX, -upperLookLimit, lowerLookLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0f, tilt);
transform.rotation *= Quaternion.Euler(0f, currentMouseDelta.x, v.z); // LINKS UND RECHTS ROTATION
Debug.Log(tilt);
}
tiltAmount is currently set to 0.7;
Meaning the Camera only tilts max. -0.7 on z or +0.7 on z.
if i am not 0 on the z axis and tilt in the opposite direction, it snappes to 0.
How do i get a smooth transition between that?
archivenine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.