x


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?

more ▼

asked Jan 17 '11 at 03:42 AM

boomcrash gravatar image

boomcrash
20 4 4 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 );

}

more ▼

answered Jan 17 '11 at 05:42 AM

Jeston gravatar image

Jeston
419 27 31 34

Here is the code I'm using. Nothing happens when I press J, any suggestions?

using UnityEngine; using System.Collections;

public class TurretRotationTest : MonoBehaviour {

private Transform currentTransform;

void Start () {
    currentTransform = transform;
}

void Update () {

    if( Input.GetKeyDown("J")) {currentTransform.localRotation *= Quaternion.AngleAxis( 10 * Time.deltaTime, Vector3.up );
    }

}

}

Jan 18 '11 at 04:23 AM boomcrash

GetKeyDown only fires once until you release, try using GetKey, did you see if your code is getting run, add some Debug.Log calls

Jan 22 '11 at 11:37 AM Jeston

@Jeston Please format your code

Feb 09 '11 at 11:50 PM fireDude67
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2245
x173
x50
x47

asked: Jan 17 '11 at 03:42 AM

Seen: 1307 times

Last Updated: Jan 17 '11 at 03:42 AM