- Home /
Two Keys ws or wd detection
I am working on a birds eye view game in which the player uses wasd to move about. However I am running into an issue with this. For example when the player presses w the character moves forward, and when the player presses a or d the character rotates left or right. I would like to have it so so when the player presses aw at the same time It would rotate/move simultaneously.
Current Key Detection:
function OnGUI () { //Set up hotkey "w" for Forward if (Event.current.Equals (Event.KeyboardEvent ("w"))) ThurstersForward();
//Set up hotkey "w" for Forward
if (Event.current.Equals (Event.KeyboardEvent ("s")))
ThurstersBackward();
//Set up hotkey "d" for rotate right
if (Event.current.Equals (Event.KeyboardEvent ("d")))
rotateRight();
//Set up hotkey "a" for rotate right
if (Event.current.Equals (Event.KeyboardEvent ("a")))
rotateLeft();
}
Current Resulting Action:
function ThurstersForward(){ rigidbody.AddForce(transform.forward); transform.Find("ThrusterA").GetComponent("EllipsoidParticleEmitter").enabled = true; transform.Find("ThrusterB").GetComponent("EllipsoidParticleEmitter").enabled = true; }
function ThurstersOff(){ transform.Find("ThrusterA").GetComponent("EllipsoidParticleEmitter").enabled = false; transform.Find("ThrusterB").GetComponent("EllipsoidParticleEmitter").enabled = false;
}
function ThurstersBackward(){ rigidbody.AddForce(transform.forward * -2); }
function rotateRight(){ Debug.Log(transform.rotation); transform.Rotate(Vector3.up * 2); }
function rotateLeft(){ transform.Rotate(Vector3.up * -2); }
Any Suggestions, Right now only one can be "active" at a time. I looked into using threads but it appeared that unity did not support them.
Answer by AngryOldMan · Apr 21, 2011 at 10:19 PM
you could use/edit the keys thats unity have set in the input manager. Instead of setting up hot keys? And use Input.GetAxis
in the fixed update function rather than each key firing off a seperate function
EDIT
if that doesnt work then something like
function FixedUpdate ()
{
if (Input.GetKey ("w"))
{
rigidbody.AddForce(transform.forward);
//stuff about your particle emitter here
}
if (Input.GetKey ("a"))
{
transform.Rotate(Vector3.up * -2);
}
//etc for the rest of your inputs
}
This doesn't seem to work unless I continuously tap the keys which is not quite what I am looking for.
Answer by burgunfaust · Apr 21, 2011 at 10:30 PM
You could just use "q" and "e" for forward left and forward right:
//Set up hotkey "q" for ForwardLeft if (Event.current.Equals (Event.KeyboardEvent ("q"))) { ThurstersForward(); rotateLeft(); }
//Set up hotkey "e" for ForwardLeft if (Event.current.Equals (Event.KeyboardEvent ("e"))) { ThurstersForward(); rotateRight(); }
Answer by Lordships · Apr 25, 2011 at 04:15 AM
I found this works great!
var rotateSpeed = .50; var thrustSpeed = 5.0;
function FixedUpdate(){ .......
transform.Rotate (Vector3.up * (Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime) );
if (Input.GetButton("Vertical") ) {
rigidbody.AddRelativeForce (Vector3.forward * thrustSpeed * Time.deltaTime);
}
}