- Home /
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);
}
Answer by Fattie · Mar 24, 2013 at 03:50 PM
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 $$anonymous$$d, i got it to work. i used this ins$$anonymous$$d:
function Update()
{
if (Input.Get$$anonymous$$ey("w"))
transform.Rotate(Vector3.forward);
if (Input.Get$$anonymous$$ey("s"))
transform.Rotate(Vector3.back);
}
thanks for the .Rotate