Basic 2D cannon rotation code not working

I have created some code to allow me to rotate a 2D cannon so that it looks up and down using the Z axis to rotate (x is my across and y is my up and down) using “w” and “s”. this code DID work but for some reason it has stop working. now only the “s” key works up until the cannon faces the other way then stops. any help with this would be great as it is driving me mad, here is my code in JavaScript:

var angle = 180;

function Update()

{

	if (Input.GetKey("w"))

		angle += Input.GetAxis("Vertical") * Time.deltaTime * 20;

	if (Input.GetKey("s"))

		angle -= Input.GetAxis("Vertical") * Time.deltaTime * -20;

	transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);

}

1 Answer

1

you may be better just using simply

#.Rotate or
.

.RotateAround

.

generally avoid quaternions unless you’re really grooving, there is little reason to use them

unityGEMS.com for many articles on these issues

never mind, i got it to work. i used this instead: function Update() { if (Input.GetKey("w")) transform.Rotate(Vector3.forward); if (Input.GetKey("s")) transform.Rotate(Vector3.back); } thanks for the .Rotate