- Home /
Smoothen Mouse Axis Input
Hey Guys! I'm currently working on a FPS experimental hobby thingy, and i've run into problem recently:
To make the whole thing look more realistc i tried to implement the movement a weapon does in every normal FPS, where the weapon rotates up to a certain amount of degrees based on the speed of your input, f.e. the speed you're giving your mouse. At the moment i've got this code:
private var start_rotation_y;
function Start() {
start_rotation_y = transform.localEulerAngles.y;
}
function FixedUpdate() {
var mouseSpeedX:float = Input.GetAxisRaw("Mouse X");
var speedRounded:float = Mathf.Round(mouseSpeedX * 3);
transform.localEulerAngles.y = start_rotation_y + speedRounded;
}
The problem with that is, that the weapon moves but the movement is very jittery, when I would rather have it being smooth and due to this realistic looking.
I've tried a few things, but they all ended up being jittery, so i hope someone in the community knows a proper fix for that!
Greets, remoteplayfreak!
Answer by testure · Jun 01, 2011 at 09:44 PM
I think that so long as you're mapping mouse input directly to your camera rotation, you're going to get some erratic behavior. have you considered doing a Quaternion.Slerp to smooth the values over time? Even if the time is as low as a tenth of a second it should reduce jittering considerably (because that's honestly how mouse smoothing works, it just averages your input over X amount of time).
Answer by remoteplayfreak · Jun 01, 2011 at 09:49 PM
I've actually tried to avoid Quaternions, because i prefer localEulerAngels, because we recently did that in school ;)
Nevertheless: Could you possibly tell me a little bit about Quaternions because i have no clue how to use them!
You can still use euler angles- but a transform rotation is actually a Quaternion. Even if you're controlling it via localEulerAngles, that's still getting converted into a Quaternion in the long run.
desiredRotation = Quaternion.Euler(new Vector3(angleX, angleY, angleZ)); // where desiredRotation is a Quaternion transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, Time.time * speed);
more info here: http://unity3d.com/support/documentation/ScriptReference/Quaternion.Slerp.html
also- post comments as comments, not as answers ;)
Sorry, I've just started using Unity Answers, and i don't know where you live, but where I live, it's not that early anymore ;)
I've got just one question: in your "transform.rotation" row, what do you use as speed?
speed is arbitrary.. it's however fast you want it to move. I'd suggest making it a public float, and then adjust the value in the inspector until it looks the way you want it to.