Hello,
I’ve reached the end point of the “Survival Animation” video. At the point of Play
I noticed that I could move while looking towards my mouse in terms of directional focus, however, the IsWalking
animation between Idle
and Move
doesn’t play. Instead the Idle
animation will play both when I’m walking and not walking.
Some things that I checked trying to resolve the error:
- I have confirmed that the
has exit time
checkbox is unchecked for all transitions. - I have test played the animation in the preview window and it seems to be functioning correctly there as well.
- I have also double checked my code below looking for errors, but haven’t found one.
At this point I hope someone on here can fill me in on what I’m missing.
Thank you in advance. I really appreciate the assist!
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLength = 100f;
void Awake () //awake is like start but is called if it's awake or not
{
floorMask = LayerMask.GetMask ("Floor");
anim = GetComponent<Animator> ();
playerRigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw ("Horizontal"); //The raw makes it an immediate to speed rather than building up.
float v = Input.GetAxisRaw ("Vertical");
Move (h, v); //Calling our functions below
Turning (); //Calling our functions below
Animating (h, v); //Calling our functions below
}
void Move (float h, float v)
{
movement.Set (h, 0.0f, v); //covers moving in just one direction
movement = movement.normalized * speed * Time.deltaTime; //deltaTime is time between each update call.
playerRigidbody.MovePosition (transform.position + movement);
}
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);//targets mouse with camera
RaycastHit floorHit;
if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
playerRigidbody.MoveRotation (newRotation);
}
}
void Animating (float h, float v)
{
bool walking = h != 0f || v != 0f; //H is not equal to 0 or is v not equal to 0. Basically did you push one of the axis. If so, we're walking.
}
}