Hello, im making a 3d top down game, and now my movement script make the player look at the mouse position in the map by a rayCast, and move around with the getAxis(“Horizontal”) and getAxis(“Vertical”).
Basically my problem its when i press W the player mover vertically, and i want him to move in the direction he is looking.
Hope someone understand my question.
Thanks.
void MovementTopDown()
{
{
moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput * moveSpeed;
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLenght;
if (groundPlane.Raycast(cameraRay, out rayLenght))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLenght);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.red);
transform.LookAt(pointToLook);
}
}
}
void FixedUpdate()
{
rb.velocity = moveVelocity;
}