Hello friends,
I am trying to animate a dog that follows the Player and sits, stands, walks, or runs depending on the distance it is from the Player.
The issue: If the Player moves away from the dog too fast, then the dog stays in the sitting position but still follows the player, making it slide on it’s bum. See the video below:
See the script below:
void Start () {
animator = GetComponent<Animator>();
}
void Update()
{
float dist = Vector3.Distance(target.transform.position, transform.position);
if (dist >= runDistance)
{
Run(animator);
}
else if (dist >= stopDistance && dist <= runDistance)
{
Walk(animator);
}
else if (dist <= sitDistance)
{
Sit(animator);
}
else {
Stand(animator);
}
}
private void Stand(Animator animator)
{
Reset();
animator.SetFloat("Movement_f", 0f);
}
private void Sit(Animator animator)
{
animator.SetBool("Sit_b", true);
}
private void Walk(Animator animator)
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, walkSpeed * Time.deltaTime);
animator.SetFloat("Movement_f", 0.5f);
}
private void Run(Animator animator)
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, runSpeed * Time.deltaTime);
animator.SetFloat("Movement_f", 1.0f);
}
private void Reset()
{
animator.SetBool("Sit_b", false);
}
}
Question: How can I get my dog to stand up before it starts to follow the Player?