First-person weapon sway?

You know how when you move your mouse very quickly, your equipped gun/sword/whatever gives a swaying motion?

I’m not sure how to do this at all, but I really need to use it for realism. The only way I can see this working is by doing some math formula to calculate the velocity of your mouse moving, which I’m not sure how to ever call such a thing in a script.

1 Answer

1

Look at Input.GetAxis, Ctrl-F for “mouse”. You can use the GetAxis() to get the delta of the mouse movement in x and y. However, it is possible that you will need the 2 axis together to perform a more accurate calculation ( Vector2.magnitude etc ). So I wrap them in a Vector2 named mouseDelta. Larger magnitude means a larger sway.

private Vector2 mouseDelta;

void Start() {
   mouseDelta = Vector2.zero;
}

void Update() {
   CalculateMouseDelta();

   //use the mouseDelta to perform all sort of calculations
}

void CalculateMouseDelta() {
   mouseDelta.x = Input.GetAxis( "Mouse X" );
   mouseDelta.y = Input.GetAxis( "Mouse Y" );
}

Thanks! So I've done my swaying using the Lerp method, but now I have one problem: My Animation clips are interfering with the lerp, so the lerp won't play. I have an idle sword animation, but if I disable my sword animations, the lerp will play. Is there any way to get around this besides scripting the animations?

Make sure that your Lerp is not using the same bone as the animation. Find/Assign empty bone that will not be used in the animation, and use that for the sway.

Thanks man! It works now!