I am using Mecanim for this project. To be clear, WASD [Walk] only begins to function if I start with WASD+SHIFT [Sneak].
The animator file is:
[SNEAK] <—> [LOCOMOTION]
^------------[IDLE]-------------^
The LOCOMOTION has an animation blend between run and walk.
The script having to do with the player movement in part is:
public class SO_PlayerMovement : MonoBehaviour
{
public float sprintSpeed;
public float normalSpeed;
public float sneakSpeed;
public float playerSpeed;
public float turnSmoother = 15f;
public float run1;
public float run2;
public int hashchecker;
private Animator moveAnim;
private SO_Hashes hashHolder;
void Awake ()
{
moveAnim = GetComponent();
hashHolder = GetComponent<SO_Hashes>();
normalSpeed = 5f;
sneakSpeed = 2.5f;
sprintSpeed = 10f;
hashchecker = hashHolder.fltSpeedHash;
//moveAnim.SetLayerWeight(0,1);
}
void FixedUpdate ()
//Calls every frame on rigidbodies to update their stuff. Using this to check for input.
{
hashchecker = hashHolder.fltSpeedHash;
float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);
run1 = x;
run2 = z;
bool bSneak = Input.GetKey(KeyCode.LeftShift); //need to actually set sneak button
bool bSprint = Input.GetKey(KeyCode.Z); //need to actually set sprint button
//SetCurrentSpeed(bSneak, bSprint);
HandleMovement(x ,z , bSneak, bSprint);
}
void HandleMovement(float xAxis, float zAxis, bool isSneaking, bool isSprinting)
//Handles all player movement factors: direction, rotation, speed, animation
{
//moveAnim.SetBool(“Sneak”, isSneaking); //Mike’s Mod to allow proper usage
moveAnim.SetBool(hashHolder.bSneakHash, isSneaking); //Mark’s original script
//moveAnim.SetBool(hashHolder.bSprintHash, isSprinting);
// ^^^ Should activate sneaking or sprinting animations when appropriate
if(xAxis != 0f || zAxis != 0f)
{
Vector3 newDirection = new Vector3(xAxis, 0f, zAxis);
Quaternion newRotation = Quaternion.LookRotation(newDirection, Vector3.up);
Quaternion incRotation = Quaternion.Lerp(rigidbody.rotation, newRotation, turnSmoother * Time.deltaTime);
rigidbody.MoveRotation(incRotation);
moveAnim.SetFloat(“Speed”, 5.7f, 0.1f, Time.deltaTime); //speed of animation
}
else
{
moveAnim.SetFloat(“Speed”, 0f);
}
}
void SetCurrentSpeed(bool isSneaking, bool isSprinting)
{
if(isSneaking)
{
playerSpeed = sneakSpeed;
}
else if(isSprinting)
{
playerSpeed = sprintSpeed;
}
else
{
playerSpeed = normalSpeed;
}
}
}