- Home /
one keypress one move in direction pressed
OK so i have decided to make a simple little game for my kids. all i want to be able to do is move the player game object once every time i press a direction key. this will then move one space ether up down left right and once moved set itself as end of the turn.
so my problem currently is that my player will move in the directions i chose but will not stop so long as i have the key pressed. this is the first time i have attempted to use a turn based system in my games,
#pragma strict
var Speed : float = 5;
var Up : KeyCode;
var Down : KeyCode;
var Right : KeyCode;
var Left : KeyCode;
function Update ()
{
if (Input.GetKey(Up))
{
rigidbody.velocity.y = Speed;
}
else if (Input.GetKey(Down))
{
rigidbody.velocity.y = Speed * -1;
}
else if (Input.GetKey(Right))
{
rigidbody.velocity.x = Speed;
}
else if (Input.GetKey(Left))
{
rigidbody.velocity.x = Speed * -1;
}
else
{
rigidbody.velocity.x = 0;
rigidbody.velocity.y = 0;
}
}
This is my current code that works great if i want a regular movment. basicly i just want the charachter to stop moving after it has completed a 1 square move in the direction i pressed. at wich point the turn would end and a bunch of things would happen to adjust score and remaining times i can move.
Format your code properly using the code tags. It's difficult to read otherwise.
Take a look at this:
http://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$eyDown.html
Answer by Alk Studios · May 31, 2014 at 11:58 PM
Use a boolean flag, once the key is pressed change the value and only reset the boolean once the keyUp function is called. Remember to check the value of the boolean when moving.
Get$$anonymous$$eyDown will not return true until the user has released the key and pressed it again. No need for any flags.
Thankyou for the help. this worked perfectly. now using Get$$anonymous$$eyDown and a speed of 50 has given me the exact results i needed. my player now moves one square in the position pressed and stops. Thanks again
Your answer
Follow this Question
Related Questions
How to change physics,lighting, and camera when a specific input is entered in-game? 2 Answers
Turn Based Battle System 6 Answers
Server software for multiplayer turn-based RPG like Pokemon 1 Answer
Help with Unity photon multiplayer turn base fighting system. 1 Answer
Best way to handle troop positioning on a Risk-like game. 1 Answer