76.7k views
3 votes
I just started game development using unity, I’m trying to control my sphere moving on a flat surface using the W,A,S,D keys, if anyone knows the code for that using C# ,please help.

1 Answer

4 votes

Answer:

Step-by-step explanation:

public class Controlador: MonoBehavior

{

// we declare velocity variable, we can change the value to get more speed

public float velocidad = 3f;

void Start()

{

}

void Update()

{

// with this condition we select the key for the movement.

// for example the left arrow or the letter "a"

if (Input . GetKey(KeyCode . LeftArrow) |I Input . GetKey("a"))

{

transform . position += Vector3 . left * velocidad * Time . deltaTime;

}

if (Input . GetKey(KeyCode . RightArrow) |I Input . GetKey("d"))

{

transform . position += Vector3 . right * velocidad * Time . deltaTime;

}

if (Input . GetKey(KeyCode . UpArrow) |I Input . GetKey("w"))

{

transform . position += Vector3 . up * velocidad * Time . deltaTime;

}

if (Input . GetKey(KeyCode . DownArrow) |I Input . GetKey("s"))

{

transform . position += Vector3 . down * velocidad * Time . deltaTime;

}

}

}

User Prithviraj Mitra
by
5.8k points