Reputation: 135
I am working on a script to switch between animations (look at the screenshot to better understand) and the script is crude but it mostly does the job. The problem is that when you go to (Walk -> WalkToSprint) the WalkToSprint boolean stays active when it should deactivate which makes the animations go in a loop.
void Update()
{
// this section is for walking and running
if (Input.GetKey(KeyCode.W))
{
anim.SetBool("Walk", true);
anim.SetBool("SprintToWalk", true);
if (Input.GetKey(KeyCode.LeftShift))
{
anim.SetBool("WalkToSprint", true);
anim.SetBool("Idle", true);
anim.SetBool("SprintToWalk", false);
}
}
// end section
//this section is for sprinting then to walking
if (Input.GetKey(KeyCode.LeftShift))
{
if (Input.GetKeyDown(KeyCode.W))
{
anim.SetBool("Sprint", true);
anim.SetBool("Walk", false);
anim.SetBool("Idle", false);
anim.SetBool("SprintToWalk", false);
}
}
///////////////////////////////////////// this section is the key up code.
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetBool("Walk", false);
anim.SetBool("Sprint", false);
anim.SetBool("Idle", false);
anim.SetBool("WalkToSprint", true);
anim.SetBool("SprintToWalk", false);
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
anim.SetBool("Idle", false);
anim.SetBool("WalkToSprint", false);
anim.SetBool("SprintToWalk", true);
}
}
Upvotes: 0
Views: 284
Reputation: 135
My solution was to change the animation of sprinting so I could remove WalkToSprint and that left me with a straight shot.
void Update()
{
if (Input.GetKey(KeyCode.W))
{
anim.SetBool("Walk", true);
if (Input.GetKey(KeyCode.LeftShift))
{
anim.SetBool("Sprint", true);
anim.SetBool("Idle", true);
}
}
/////////////////////////////////////////
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetBool("Walk", false);
anim.SetBool("Idle", false);
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
anim.SetBool("Sprint", false);
}
}
Upvotes: 0
Reputation: 15941
Note that your "walk to sprint" state is completely and utterly incapable of sending the animation to the sprint state. This might not be your only problem, however, but this is almost certainly the source of the infinite loop problem as the state machine is constantly sent back to Walk after being in WalkToSprint and never able to reach the Sprint state.
I would also advise reworking your logic code, because the section labeled "for sprinting then to walking" requires that the user initiate a press down on the W key (something that they can't do if they're already sprinting, because Sprinting is holding both shift and W in the down position, which also calls tells the animation to go idle anim.SetBool("Idle", true);
!).
Upvotes: 1