Weapon sway bug

Hello everybody! So I have a small problem with a script which add some movements to the gun when you move your mouse around, the problem is that the gun rotate to much when I move the mouse, for exemple, see this screenshot, it show what happen when I move the mouse to the left, it’s the same for the right, up and down:

And here is the code:

var speed : float;

private var moveOnX : float;
private var moveOnY : float;

private var originPos : Vector3;

function Start() {
	originPos = transform.localPosition;
}

function FixedUpdate () {

	moveOnX = Input.GetAxis( "Mouse X" ) * Time.deltaTime;
	moveOnY = Input.GetAxis( "Mouse Y" ) * Time.deltaTime;

	var lookAt : Vector3 = originPos - Vector3(originPos.x + moveOnX * Time.deltaTime, originPos.y + moveOnY * Time.deltaTime, originPos.z);

	transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.LookRotation(lookAt), speed * Time.deltaTime);

}

Everything else works perfectly, it have a smooth rotation and all, but it rotate way to much! Can anybody help me please? Thank you a lot!

originPos.x + moveOnX * Time.deltaTime

moveOnX = Input.GetAxis( “Mouse X” ) * Time.deltaTime;

substitue moveOnX code in place of the variable name…

originPos.x + Input.GetAxis( “Mouse X” ) * Time.deltaTime * Time.deltaTime

you’re multiplying by Time.deltaTime twice…

Thanks! I forgot to remove this, this is something I wrote when I was trying to fix the bug, so I removed it but it doesn’t fix my bug! Here is my new code:

var speed : float;
var multiplier : float;

private var moveOnX : float;
private var moveOnY : float;

private var originPos : Vector3;

function Start() {
	originPos = transform.localPosition;
}

function FixedUpdate () {

	moveOnX = Input.GetAxis( "Mouse X" ) * Time.deltaTime;
	moveOnY = Input.GetAxis( "Mouse Y" ) * Time.deltaTime;

	var lookAt : Vector3 = originPos - Vector3(originPos.x + moveOnX, originPos.y + moveOnY, originPos.z);

	transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.LookRotation(lookAt), speed * Time.deltaTime);

}

Thank you!

Saw this looking for something else. Future warnings intended. Never multiply your camera rotation factor (from axis like mouse) by Time.deltatime. Time.deltatime is a factor to fit the movement into FPS basis. If you want your player to move 10 units per second, you do 10*Time.deltaTime; You’re essentially trying to do MousePosition = Cursor change per second; Cursor Change resets every frame! To fix this script appropriately, remove the Time factors on line 15/16, go into your unity input settings for Mouse X and Mouse Y, and lower the sensitivity. Alternatively, create a mouse sensitivity buffer (0-1 based) to multiply against the the GetAxis results from mouse input. Although this may play perfectly find in most cases you run it in, as soon as another computer, or device tries to run this, the camera will move far slower, or faster, depending on the system specs. If you want to quick-test these results, run it without vsync.

This is what I use for my weapon sway…

	void WeaponSway() {
        float movementY = Input.GetAxis("Mouse X") * swayAngle;
        float movementX = Input.GetAxis("Mouse Y") * swayAngle;
        Quaternion targetVect = Quaternion.Euler(-movementX, movementY, 0);
        transform.localRotation = Quaternion.Slerp(transform.localRotation, targetVect, Time.deltaTime / 0.1f); 	
	}

hope this helps

EDIT: btw to use this you would just do…

	void Update () {
		WeaponSway();
	}

or you could just put the code directly into your update function, but I like to make separate functions for cleaner code.