How do I make my movement script move the character UNTIL I let go of the key?
Hello, I am quite new to Unity in general and was wondering how I can this movement script move the object until I let go of the key. So far, if I hold A or W, it moves the object 1 step at the desired speed, but only once. I have to let go of the key and press it again to repeat the process.
using UnityEngine;
using System.Collections;
public class controlScript : MonoBehaviour {
//Variable declaritions
static float speed = 5;
static float step = 1;
private Vector3 pos;
void Start () {
pos = transform.position;
}
//Actual Control
void Update () {
if (Input.GetKeyDown(KeyCode.D))
{
pos.x += step;
}
if (Input.GetKeyDown(KeyCode.A))
{
pos.x -= step;
}
transform.position = Vector3.MoveTowards(transform.position, pos, speed * Time.deltaTime);
}
}
Thank you in advance
Answer by dkjunior · Oct 07, 2015 at 07:12 PM
You can use Input.GetKey(KeyCode.D) instead. GetKeyDown only fires on the first frame when they key got pressed, GetKey will be fired on each frame while the key is pressed.
Your answer
Follow this Question
Related Questions
How do i use the MathF.Clamp Function correctly? 1 Answer
How to make a FreeLook camera 1 Answer
Set public gameobject by raycast hit target 1 Answer
my script was written but does not want to work 1 Answer
Move an object move from point a to point b to point c to point d and back to point a? 2 Answers