- Home /
Camera Tilt Smooth [Help Needed]
Hi I got this first person camera tilt script. It tilt's to the left and right when moving horizontally. Any idea how I can make this smooth / tilt slower instead of it just snapping to the angle. Heres the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPTilt : MonoBehaviour
{
public float _tiltAmount = 5;
public KeyCode _leftBtn = KeyCode.A;
public KeyCode _rightBtn = KeyCode.D;
void Update () {
if (Input.GetKeyDown (_leftBtn)) {
this.transform.Rotate (0, 0, _tiltAmount);
} else if (Input.GetKeyUp (_leftBtn)) {
this.transform.Rotate (0, 0, -_tiltAmount);
}
if (Input.GetKeyDown (_rightBtn)) {
this.transform.Rotate (0, 0, -_tiltAmount);
} else if (Input.GetKeyUp (_rightBtn)) {
this.transform.Rotate (0, 0, _tiltAmount);
}
}
}
Answer by Hellium · May 13, 2021 at 10:15 PM
Code not tested (and written from mobile)
public class FPTilt : MonoBehaviour
{
public float _tiltAmount = 5;
public KeyCode _leftBtn = KeyCode.A;
public KeyCode _rightBtn = KeyCode.D;
public float _rotationSpeed = 1;
private Quaternion _initialRotation;
private Quaternion _targetRotation;
void Start()
{
_initialRotation = _targetRotation = transform.rotation;
}
void Update () {
if (Input.GetKeyDown (_leftBtn)) {
_targetRotation = Quaternion.Euler(0, 0, _tiltAmount ) * _initialRotation;
} else if (Input.GetKeyUp (_leftBtn)) {
_targetRotation = _initialRotation;
}
if (Input.GetKeyDown (_rightBtn)) {
_targetRotation = Quaternion.Euler(0, 0, -_tiltAmount ) * _initialRotation;
} else if (Input.GetKeyUp (_rightBtn)) {
_targetRotation = _initialRotation;
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, _rotationSpeed * Time.deltaTime);
}
}
Thanks for you answer! The tilting works really smooth with your script however the camera unfortunatley now continiously rotates slowly.
I've just tested the code and it does not seem to have any issue except being very slow, but I changed the rotation value in the inspector.
Alright I looked again and disabled my playercontroller script and it worked fine. Look's like the camerarotation is causing it used for looking.
Your answer
![](https://koobas.hobune.stream/wayback/20220613033715im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to create scaled space? 0 Answers
Children Of Player objects jitter 0 Answers
Edit Render Camera programmatically 1 Answer