When I am running forward and I point to the bottom of the screen the player points in that direction but the animation doesn’t switch to running backwards. Same for any other direction. I want my animations to be relative to the direction my mouse is pointing.
I have narrowed the problem down to what the script sends to the Animator component
What do I need to change for this to work?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLength = 100f;
void Awake ()
{
floorMask = LayerMask.GetMask ("Floor");
anim = GetComponent <Animator> ();
playerRigidbody = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h, v);
Turning ();
Animating (h, v);
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
}
void Turning ()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit floorHit;
if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);
playerRigidbody.MoveRotation (newRotatation);
}
}
void Animating (float h, float v)
{
anim.SetFloat("MovementZ",v);
anim.SetFloat("MovementX",h);
}
}