Hi, I’m using a 2D freeform directional blend tree for my animation and it works fine but the problem is that the animation does not play relative to the players rotation. So i somehow need to take the rotation into count when playing the animation.
Quick example: if the player has rotated 90 degrees and press W the character will move up as it should but it does so doing the walk animation forward which looks weird it should know the player is facing a different direction and instead play the sidewalk animation.
I made a very simple sketch to show my problem more clearly
(img wont work so heres the link)
Here’s my code for moving, turning the player and animation.
void FixedUpdate()
{
// Store the input axes.
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
// Move the player around the scene.
Move(h, v);
// Turn the player to face the mouse cursor.
Turning();
// Animate the player.
Animating(h, v);
}
void Move(float h, float v)
{
// Set the movement vector based on the axis input.
movement.Set(h, 0f, v);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition(transform.position + movement);
}
void Turning()
{
// Create a ray from the mouse cursor on screen in the direction of the camera.
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
// Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
Vector3 playerToMouse = floorHit.point - transform.position;
// Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f;
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
// Set the player's rotation to this new rotation.
playerRigidbody.MoveRotation(newRotation);
}
}
void Animating(float h, float v)
{
anim.SetFloat("Horizontal", h, 0.05f, Time.deltaTime);
anim.SetFloat("Vertical", v, 0.05f, Time.deltaTime);
}
}