- Home /
How can make the player move constantly when the key A or D is held down?
I can only move the player a little bit per keypress. Is there a way i can make it so that I constantly move, for as long as A or D is held down?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
void Update () {
if (Input.GetKeyDown (KeyCode.A))
{
transform.Translate (-Vector3.right * Time.deltaTime * speed);
}
if (Input.GetKeyDown (KeyCode.D))
{
transform.Translate (-Vector3.right * Time.deltaTime * speed);
}
}
}
Try using Get$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown
Answer by kchhibber · Jun 21, 2017 at 09:24 PM
Try using GetKey instead of GetKeyDown.
GetKeyDown returns true during the frame the user starts pressing down the key identified by name.
GetKey returns true while the user holds down the key identified by name. Think auto fire.
Here is the link to GetKey scripting API: https://docs.unity3d.com/ScriptReference/Input.GetKey.html
Your answer
Follow this Question
Related Questions
Performing sequential actions like moving and dealing damage 1 Answer
why tansform.Translate is not working? 1 Answer
How do i maintain the same speed in the air? 1 Answer
[SOLVED] How to still move the character but with different keys? 0 Answers
[ C#] Character Movement based on Camera Direction. 2 Answers