How to move the Character using WASD

Hi!

First of all, I dont want to use the FPS Controller Prefab and a C# script (peace hahaha)

So how do I move my character to go forward where Im currently looking at.

Thnks!

Error 404 : No effort found

3 Answers

3
int speed = 10;
  
void Move()
{
    Vector3 moveVec = new Vector3( Input.GetAxis("Horizontal") , 0 , Input.GetAxis("Vertical") );
    player.transform.position += moveVec * speed * Time.DeltaTime;
}

Make sure the Project SettingsInput has "Horizontal" and "Vertical" axes assigned to WASD/ Arrow keys.

public float speed =20; public float turnSpeed = 50f; void Update () { if (Input.GetKey (KeyCode.W)){ transform.Translate(Vector3.forwardspeedTime.deltaTime); } if (Input.GetKey (KeyCode.S)){ transform.Translate(-Vector3.forwardspeedTime.deltaTime); } if (Input.GetKey (KeyCode.D)){ transform.Rotate(Vector3.up,turnSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.A)){ transform.Rotate(Vector3.up,-turnSpeed * Time.deltaTime); } }

No, because transform.right is different from Vector3.right. It changes when the object rotates.

Here is my code with character controller. It doesn’t have gravity by default so I added it. I assume you want it for a 3d game

public float speed = 5;
public float gravity = -5;

float velocityY = 0; 

CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    velocityY += gravity * Time.deltaTime;

    Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0 Input.GetAxisRaw("Vertical"));
    input = input.normalized;

    Vector3 temp = Vector3.zero;
    if (input.z == 1)
    {
        temp += transform.forward;
    }
    else if (input.z == -1)
    {
        temp += transform.forward * -1;
    }

    if (input.x == 1)
    {
        temp += transform.right;
    }
    else if (input.x == -1)
    {
        temp += transform.right * -1;
    }

    Vector3 velocity = temp * speed;
    velocity.y = velocityY;
    
    controller.Move(velocity * Time.deltaTime);

    if (controller.isGrounded)
    {
        velocityY = 0;
    }
}

This will also work with a controller. The main advantage of using a character controller over rigidbody is it can go up slopes.
Also, if you want jumping, it’s simple enough, the axis is Input.GetAxisRaw(“Jump”), and just reset velocityY to how far you want to jump

is it JS or C#?

You can replace the if-statement with temp += transform.forward * input.z; temp += transform.right * input.x; This will allow you to strafe and move forwards/backwards at the same time.

Try this:

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);
}

Don’t forget to attach a Rigidbody to your object and make sure script is attached to object that is going to move.

JS or C#???

Isnt this about rolling a ball? I need the FPS controller one

How do I add the object? (And can I have another script to move to camera with mouse?)

Please help me do this https://www.youtube.com/watch?time_continue=42&v=df5G8s-KVQ4.