I am trying to move my character, but as long as i press the arrow key, he keeps moving.
I want him to move in a direction for a particular distance at a press of an arrow key and stop.
he must again move when next arrow key is pressed.
I wrote the following code.
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour
{
float playerSpeed = 5.0f;
void Start ()
{
transform.position = new Vector3 (-3, 3, -2);
}
void Update ()
{
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime);
transform.Translate(Vector3.up * Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime);
}
}
You’re using Update to look at the Axis which means as long as the Axis is in use Update will apply the translate every frame.
What it sounds like you want is for something to watch for a KeyDown rather than an axis which are used more for steady movement. On the KeyDown (up arrow or whatever) translate a Vector3 (newPosition) you can predefine separate from the player as where the player needs to go. The use Vector3.Lerp(player.transform.position, newPosition, playerSpeed) in Update() to move the player closer each frame.
In this case, if the player whams on the up arrow a bunch it will translate the newPosition Vector3 many times, but the player will still take time (dependant on the playerSpeed you’ve defined) to Lerp closer.
you’ll probably also want player speed to be a lot less, or make the code in Lerp more like playerSpeed * .01 because 5f per frame is a lot of allowed movement.
as i understood you want your character to move for a short distance and then stop, then another key press is required to move him again. if that what you want then you can simply you can declare a variable first to simulate the momentum associated with the key press then decrease its value each frame until another key is pressed, here is the code (not tested but should work)
public float speed = 5f;
public float momentum = 0;
public float damping = 5;
void Update()
{
float momentumSpeed = 5f; // initial speed that is added to the character
float damping = 5; // increase this value to stop quickly
bool arrowKeysUp = true; // boolean flag to indicate that the arrow keys arn't pressed
float momentum = 0;
// if user press any arrow key and you wern't press the any arrow key the last frame
if (Input.GetAxis("Horizontal") != 0f || Input.GetAxis("Vertical") != 0f && arrowKeysUp == true)
{
momentum = momentumSpeed;
arrowKeysUp = false;
}
else
arrowKeysUp = true;
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * momentum * Time.deltaTime);
transform.Translate(Vector3.up * Input.GetAxis("Vertical") * momentum * Time.deltaTime);
if (momentum > 0f)
// decrease the momentum each frame
momentum -= (damping * Time.deltaTime);
}
You could use a float value to cool down from when the Key/Button was last pressed heres an example.
public float CD = 3; // public so u can see it in inspector but this value can be private / //This is your CoolDown
public bool doCD; // can also be private, This will tell your cooldown to count.
void Update() {
if(Input.GetKeyDown(Keycode.W) && CD <= 0) { // check ur keypress and if ur Cooldowns at 0
transform.position += Vector3.forward * 10; // the actual movement applied.
CD = 3; // reset the count of the cooldown
doCD = true; // tell our cooldown to count
}
if(doCD) { // check if we should be cooling
CD -= 1 * time.deltaTime; // subtracting time from the cooldown
}
if(CD < 0) {
doCD = false; // turning off the cooldown when it hits 0
CD = 0;
}
}