0

I don't know why but instead of moving smoothly my player is not kinda teleporting but i have to press the key multiple times in order to move him a few inches, i can't hold the key for him to walk. here's my code:

public class player : MonoBehaviour
{
private Rigidbody _rigidbody;

private void Awake() => GetComponent< Rigidbody> ();


private void Start()
{
  

 

}

void Update()
{

  if (_WalkRight) 
   transform.position += Time.deltaTime * Walk_speed * Vector3.right;

  if (_WalkLeft) 
   transform.position += Time.deltaTime * Walk_speed * Vector3.left;

  if (_WalkUp) 
   transform.position += Time.deltaTime * Walk_speed * Vector3.forward;

  if (_WalkDown) 
   transform.position += Time.deltaTime * Walk_speed * Vector3.back;

  
}

[SerializeField] private float Walk_speed = 30f;
[SerializeField] private float Run_speed = 60f;
private bool _WalkRight => Input.GetKeyDown(KeyCode.D);
private bool _WalkLeft => Input.GetKeyDown(KeyCode.A) ;
private bool _WalkUp => Input.GetKeyDown(KeyCode.W) ;
private bool _WalkDown => Input.GetKeyDown(KeyCode.S);


}

Can someone point me out what i'm doing wrong? also i know the variables usually goes in the beginning of the code, i'll fix it.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48

1 Answers1

1

Input.GetKeyDown(KeyCode) doesn't return true if you hold it on the next frame, i.e. you need to repeatedly press the desired key.

Use Input.GetKey(KeyCode) instead.

Raildex
  • 3,406
  • 1
  • 18
  • 42