Hi,
I have a blend tree in mecanim set up with four animations, walking back, walking forward, running forward and sprinting forward. When I start moving forward or backward it smoothly transitions into the correct animation depending on if run is toggled on and/or if the sprint button is pressed. However if I am already moving forward and I toggle run or sprint it jumps to that new animation. I’m using a heavily modified version of the bot control script.
void Update()
{
//Toggle run on or off
if(Input.GetButtonDown("RunToggle"))
{
if(run == 1f)
{
run = 0f;
}
else
{
run = 1f;
}
}
//Hold sprint to keep it on or let go to turn it off
if(Input.GetButton("Sprint"))
{
if(run == 1f)
{
sprint = 1f;
}
else
{
sprint = 2f;
}
}
else
{
sprint = 0f;
}
}
void FixedUpdate ()
{
//Mouse look on X axis. Looking accross the X axis means rotating on the Y axis. This is mirrored in the PlayerLook script
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
transform.localEulerAngles = new Vector3(0, rotationX, 0);
float h = Input.GetAxis("Horizontal"); // setup h variable as our horizontal input axis
float v = Input.GetAxis("Vertical"); // setup v variables as our vertical input axis
//Add the values of the player's speed, if they are running and sprinting and times it by the forward value (1 or 0).
float runSpeed = (1f + run + sprint) * v;
float strafeSpeed = (1f + run) * h;
anim.SetFloat("ForwardSpeed", runSpeed); // set our animator's float parameter 'ForwardSpeed' equal to the vertical input axis
anim.SetFloat("StrafeSpeed", strafeSpeed); // set our animator's float parameter 'StrafeSpeed' equal to the horizontal input axis
anim.speed = animSpeed; // set the speed of our animator to the public variable 'animSpeed'
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
if(anim.layerCount ==2)
layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1); // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
}
As you can see I set the run and sprint to a 1 or 0 (sprint also has 2 if run is off) and then add them together at the bottom with with 1 (that will later be replaced with a speed variable for character customisation) then mulitplied by the state of the horizontal axis. What I think is hapening is that the because v is an axis it is already being lerped. But I don’t know where that is. I also tried making a lerp based on delta time inside update for each of the transitions (toggling sprint or run) but it didn’t do anything (perhaps lerp can only be used in late update?)
Any help you guys can offer would be greately appreciated!
Kind regards,