¿Como mover a mi personaje segun la direccion a la que mire? How do I move my character according to the direction they're facing?

Durante un tiempo llevo queriendo hacer un juego en 3D en primera persona, para hacer esto he seguido varios tutoriales que me podían orientar y cuando he conseguido tener el movimiento de la camara y el movimiento de mi personaje me he dado cuenta que el personaje no se mueve según esté orientado, por el contrario se mueve siempre según las coordenadas del mapa.

Translation by Mod

For a while I’ve been wanting to make a first-person 3D game. I’ve been following several tutorials to guide me. Now that I’ve managed to get the camera movement and my character’s movement working, I’ve realized that the character doesn’t move according to the way they’re oriented. Instead, they always move according to the map’s coordinates.

@SrSopa_13 - Puedes hacer que tu personaje se mueva en la dirección a la que está mirando utilizando el transform del personaje y la dirección en la que está mirando. Aquí te muestro cómo puedes hacerlo:

Primero, tendrías que calcular la dirección del movimiento relativo a la orientación de la cámara. Esto se hace tomando la entrada del jugador (usualmente mapeada a las teclas WASD) y rotándola en relación a la orientación de la cámara. Aquí tienes un ejemplo de cómo hacerlo:

Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
direction = Camera.main.transform.TransformDirection(direction);
direction.y = 0;

A continuación, puedes mover tu personaje usando esta dirección y multiplicándola por la velocidad de movimiento de tu personaje y el tiempo transcurrido desde el último frame para un movimiento suave:

float speed = 5f; // Modifica esto con la velocidad de movimiento de tu personaje
characterController.Move(direction * speed * Time.deltaTime);

Por último, si quieres que tu personaje siempre mire en la dirección en la que se está moviendo, puedes hacerlo de la siguiente manera:

if (direction != Vector3.zero)
{
    characterController.transform.forward = direction;
}

Este código garantiza que tu personaje siempre mire en la dirección en la que se está moviendo. Espero que esto te ayude a resolver tu problema. ¡Buena suerte con tu desarrollo!

===================English====================

You can make your character move in the direction they are facing by using the character’s transform and the direction they are looking. Here is how you can do this:

First, you would need to calculate the direction of movement relative to the camera’s orientation. This is done by taking the player’s input (usually mapped to the WASD keys) and rotating it relative to the camera’s orientation. Something like this:

Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
direction = Camera.main.transform.TransformDirection(direction);
direction.y = 0;

Next, you can move your character using this direction and multiplying it by your character’s movement speed and the time elapsed since the last frame for smooth movement:

float speed = 5f; // Adjust this to your character's movement speed
characterController.Move(direction * speed * Time.deltaTime);

Finally, if you want your character to always face in the direction they are moving, you can do so in the following way:

if (direction != Vector3.zero)
{
    characterController.transform.forward = direction;
}

This code ensures that your character always faces in the direction they are moving. I hope this helps you solve your problem. Good luck with your development!