Reputation: 1773
If I don't bump the model (not the player) walking around the scene the script works fine.
But if I bump the model with my character controller the model will continue to move once it hits the waiting animation state.
Why would that happen? How can i fix it?
public class MoveMan2 : MonoBehaviour
{
public float speed = 0.85f;
public Animator anim;
void Start()
{
anim = GetComponent<Animator>();
InvokeRepeating("SetWalkMode",5.0f,20.0f);
}
void SetWalkMode()
{
anim.Play("SkitsWalk", -1, 0f);
}
void Update()
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("SkitsWalk"))
{
Debug.Log("Skits walking");
transform.Translate(0, 0, speed * Time.deltaTime);
}
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Wait"))
{
Debug.Log("Waiting");
}
}
}
Upvotes: 0
Views: 1377
Reputation: 28
The 3rd parameter of InvokeRepeating is how often the method you specified will be called again. So Unity will call SetWalkMode each 20 seconds. When your model will enter its "wait" state it will be reset by this method in 20 seconds.
You could set a variable (like a bool) when you detect a collision and check for that variable in your SetWalkMode method.
bool isObjectInCollision = false;
void EnterCollision()
{
// do something
isObjectInCollision = true;
}
void LeaveCollision()
{
// do something
isObjectInCollision = false;
}
void SetWalkMode()
{
if (isObjectInCollision)
return;
anim.Play("SkitsWalk", -1, 0f);
}
Upvotes: 1