- Home /
Turret Rotation Controlled by Arrow Keys
I have a tank navigation working with the WASD keys and would like to Rotate the Turret using the Arrow keys, Forward Arrow = North, Backward Arrow = South, Left Arrow = East, Right Arrow = West.
I'm not sure the of the method that I should use to approach this, any suggestions?
Answer by Jeston · Jan 17, 2011 at 05:42 AM
In update(), test if the key is down and apply a localrotation to your turret's rotatable mesh
something like this:
if( Input.GetKeyDown("J" ) ) { turrentTransform.localRotation = Quaternion.Angleaxis( 10 Time.DeltaTime, Vector3.up );
}
Here is the code I'm using. Nothing happens when I press J, any suggestions?
using UnityEngine; using System.Collections;
public class TurretRotationTest : $$anonymous$$onoBehaviour {
private Transform currentTransform;
void Start () {
currentTransform = transform;
}
void Update () {
if( Input.Get$$anonymous$$eyDown("J")) {currentTransform.localRotation *= Quaternion.AngleAxis( 10 * Time.deltaTime, Vector3.up );
}
}
}
Get$$anonymous$$eyDown only fires once until you release, try using Get$$anonymous$$ey, did you see if your code is getting run, add some Debug.Log calls
Answer by hugiel46 · Sep 27, 2016 at 10:40 AM
What you can try if you want this is create a float called 'Keyheld' and add this script into update.
if (Input.GetKeyUp(j)) { KeyHeld = 1; } if (Input.GetKeyUp("j")) { KeyHeld = 0; } <
then do
if(Keyheld == 1) { //insert your code here// }
<
Your answer
![](https://koobas.hobune.stream/wayback/20220613095753im_/https://answers.unity.com/themes/thub/images/avi.jpg)