- Home /
Camera shaking and barely moving after adding a clamp to x rotation?
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public int Sensitivity;
private float MouseYSet;
void Start () {
MouseYSet = Input.mousePosition.y;
}
void Update () {
transform.localRotation = Quaternion.Euler (Mathf.Clamp (transform.rotation.x, -90, 90), 0, 0);
if (Input.mousePosition.y > MouseYSet)
{
transform.Rotate (new Vector3 (-1, 0, 0) * (Input.mousePosition.y - MouseYSet) * Sensitivity * 10 * Time.deltaTime);
MouseYSet = Input.mousePosition.y;
}
if (Input.mousePosition.y < MouseYSet)
{
transform.Rotate (new Vector3 (-1, 0, 0) * (Input.mousePosition.y - MouseYSet) * Sensitivity * 10 * Time.deltaTime);
MouseYSet = Input.mousePosition.y;
}
}
}
Answer by WillNode · Dec 23, 2014 at 10:48 PM
you are better to use Input.GetAxisRaw() Instead.
replace your Update()
function with this:
float y = 0f;
void Update(){
y-=Input.GetAxisRaw ("Mouse Y") * Sensitivity * 10 * Time.deltaTime;
y=Mathf.Clamp (y, -90, 90);
transform.rotation = Quaternion.Euler(y, 0, 0);
}
I didn't test the code. but hope it will help
One more question, why do you use the - symbol? Is it because when the mouse is on the right its negative and should be positive? if so, what makes it negative?
Answer by conceptfac · Oct 17, 2020 at 11:01 PM
Still shaking!
Don't use Time.deltaTime with "$$anonymous$$ouse X" or "$$anonymous$$ouse Y" as those are already delta values. $$anonymous$$ultiplying those by deltaTime will makes it frame rate dependent. This is actually mentioned in the documentation, though not explained very clearly.
If the axis is mapped to the mouse, the value is different and will not be in the range of -1...1. Instead it'll be the current mouse delta multiplied by the axis sensitivity. Typically a positive value means the mouse is moving right/down and a negative value means the mouse is moving left/up.
This is frame-rate independent; you do not need to be concerned about varying frame-rates when using this value.
Your answer
Follow this Question
Related Questions
Tracking a projectile with Mathf.Clamp 1 Answer
How to use Mathf.Clamp with child objects? 0 Answers
Strange vibrating when moving camera view 0 Answers
Clamping values for camera bounds 1 Answer
clamp rotation the other way? 1 Answer