c# wasd movement controls code example

Example 1: c# wasd movement controls

private void Update() {        
       Rigidbody rb = GetComponent<Rigidbody>();
     if (Input.GetKey(KeyCode.A))             
       rb.AddForce(Vector3.left);        
       if (Input.GetKey(KeyCode.D))             
         rb.AddForce(Vector3.right);         
       if (Input.GetKey(KeyCode.W))             
         rb.AddForce(Vector3.up);         
       if (Input.GetKey(KeyCode.S))             
         rb.AddForce(Vector3.down);      }

Example 2: c# wasd movement controls

using UnityEngine;

public class movecube : MonoBehaviour
{
    public float speed = .1f;


    // Update is called once per frame
    void Update()
    {
        float xDirection = Input.GetAxis("Horizontal");
        float zDirection = Input.GetAxis("Vertical");

        Vector3 moveDirection = new Vector3(xDirection, 0.0f, zDirection);

        transform.position += moveDirection * speed;
    }
}