How to adjust a GameObject's rotation to face it's moving direction ?
A little visualisation, i'm working on a top-down Diablo style game, the character movement is handled by Input.GetAxis and RigidBody.MovePosition, and it's rotation is handled by Quaternion.LookRotation and RigidBody.MoveRotation, so both movement and rotation work.
My Problem i couldn't manage to get him moving in the "Forward" direction, or essentially the direction of his movement
void GetInput()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
mousePosition = Input.mousePosition;
moveInput = new Vector3(horizontal, 0f, vertical);
if (moveInput.magnitude > 1)
moveInput.Normalize();
}
void Move()
{
rigidBody.MovePosition(rigidBody.position + moveInput * moveSpeed * Time.fixedDeltaTime);
}
void Rotate()
{
Ray ray = Camera.main.ScreenPointToRay(mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit, 1000f))
{
moveDir = hit.point - rigidBody.position;
moveDir.y = 0f;
moveDir.Normalize();
rotation = Quaternion.LookRotation(moveDir, rigidBody.transform.up);
rigidBody.MoveRotation(rotation);
}
}
}
been trying multiple approaches to this problem but none worked thus far, any insight will be appreciated
Your answer
Follow this Question
Related Questions
Moving forward based off of Rotation 3 Answers
How can I incorporate a Rotation Towards the Mouse Position into this Script? I Tried. 0 Answers
Get player to face the direction of movement 1 Answer
Character movement that ends with a specific facing 0 Answers
Struggling to get the rotation the player is moving in. 1 Answer