X
X
xlo2402021-03-29 18:32:57
Unity
xlo240, 2021-03-29 18:32:57

What script is needed to move the character from the keyboard?

Tell me a script or lesson for character movement from the keyboard. So that the control is like: W - forward, S - back, A and D - turn the character on the spot. Classically CharacterController does not provide this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
genadievivan05, 2021-03-29
@genadievivan05

Try it.

public GameObject obj;
public float range = 5f, moveSpeed = 3f, turnSpeed = 40f; //значения можно менять
void Update ()
 {

if(Input.GetKey(KeyCode.UpArrow))
      obj.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); // движения вверх.

    if(Input.GetKey(KeyCode.DownArrow))
      obj.transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime); //движения в низ

    if(Input.GetKey(KeyCode.LeftArrow))
      obj.transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime); // поворот в право

    if(Input.GetKey(KeyCode.RightArrow))
      obj.transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime); //поворот влево
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question