i have written a code to allow my player to animate according to 1d blend tree. everything is fine. but as soon as i use Lshift in my script to animate run. the animation never fade back to zero(ideal). i want to player to animate walk animation when “horizontal and vertical button” are pressed. and use run animation when “horizontal and vertical button + leftshit” is pressed
![public class movement_rotation : MonoBehaviour
{
public float InputX;
public float InputZ;
public Vector3 desiredMoveDirection;
public bool blockRotationPlayer;
public float desiredRotationSpeed;
public Animator anim;
public float Speed;
public float allowPlayerRotation;
public Camera cam;
public CharacterController controller;
public bool isGrounded;
private float verticalVel;
private Vector3 moveVector;
public float walkSpeed;
public float runspeed;
public bool Lshift;
// Use this for initialization
void Start()
{
anim = this.GetComponent<Animator>();
cam = Camera.main;
controller = this.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
InputMagnitude();
isGrounded = controller.isGrounded;
if (isGrounded)
{
verticalVel -= 0;
}
else
{
verticalVel -= 2;
}
moveVector = new Vector3(0, verticalVel, 0);
}
void PlayerMoveAndRotation()
{
InputX = Input.GetAxis("Horizontal");
InputZ = Input.GetAxis("Vertical");
var camera = Camera.main;
var forward = cam.transform.forward;
var right = cam.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
desiredMoveDirection = forward * InputZ + right * InputX;
if (blockRotationPlayer == false)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMoveDirection), desiredRotationSpeed);
}
}
void InputMagnitude()
{
Lshift = Input.GetKey(KeyCode.LeftShift);
//calculate input
InputX = Input.GetAxis("Horizontal");
InputZ = Input.GetAxis("Vertical");
anim.SetFloat("InputZ", InputZ, 0.0f, Time.deltaTime * 2f); //from animator float of InputX is called
anim.SetFloat("InputX", InputX, 0.0f, Time.deltaTime * 2f); //from animator float of InputX is called
//calculate the input magnitude
Speed = new Vector2(InputX, InputZ).sqrMagnitude;
if (Speed > allowPlayerRotation)
{
if (Lshift)
{
transform.Translate(Vector3.forward * Speed * runspeed * Time.deltaTime);
anim.SetFloat("InputMagnitude", Speed * 1.0f, 0.0f, Time.deltaTime);
PlayerMoveAndRotation();
}
else if (Lshift = false)
{
transform.Translate(Vector3.forward * Speed * walkSpeed * Time.deltaTime);
anim.SetFloat("InputMagnitude", Speed * 0.5f, 0.0f, Time.deltaTime);
PlayerMoveAndRotation();
}
}
else if (Speed < allowPlayerRotation)
{
anim.SetFloat("InputMagnitude", Speed, 0.0f, Time.deltaTime);
}
}
}][1]