2
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimatorController : MonoBehaviour
{
    public Animator[] animators;
    public Transform target;
    public float speed = 1f;

    // Use this for initialization
    void Start()
    {
        for (int i = 0; i < animators.Length; i++)
        {
            animators[i].SetFloat("Walking Speed", speed);
        }
    }

    // Update is called once per frame
    void Update()
    {
        float distanceFromTarget = Vector3.Distance(animators[2].transform.position, target.position);
        if (distanceFromTarget < 15)
        {
            float speed = (distanceFromTarget / 15) / 1;
            for (int i = 0; i < animators.Length; i++)
            {
                animators[i].SetFloat("Walking Speed", speed);
            }
        }

        if(distanceFromTarget < 1)
        {
            for(int i = 0; i < animators.Length; i++)
            {
                animators[i].SetFloat("Walking Speed", 0);
            }
        }
    }
}

This part of the slowdown is working fine:

float distanceFromTarget = Vector3.Distance(animators[2].transform.position, target.position);
            if (distanceFromTarget < 15)
            {
                float speed = (distanceFromTarget / 15) / 1;
                for (int i = 0; i < animators.Length; i++)
                {
                    animators[i].SetFloat("Walking Speed", speed);
                }
            }

I tried to add this part to make the characters stop:

if(distanceFromTarget < 1)
            {
                for(int i = 0; i < animators.Length; i++)
                {
                    animators[i].SetFloat("Walking Speed", 0);
                }
            }

But they never stop. They are slowing down then continue walking slow and then the speed moving up again once they are getting away from the target.

I want them to slowdown when the distance is 15 and less and once animators[2] distance is 1 from target stop.

Screenshot of the Animator: The two soldiers have the same animator controller the other character have it's own controller all of them have the same states and parameters and same settings:

Animator

\$\endgroup\$
1
\$\begingroup\$

Distances and bounding boxes

I think that the problem is that the if-statement of the second part of your script is not getting triggered.

For distanceFromTarget < 1 to become true, the distance between animators[2].transform.position and target.position has to become less than 1. The problem is that these positions are actually two points. Most likely, they are the coordinates of the cores of the objects. You are checking the distance between these position values of the objects, not the actual distance between them. If you have some game-logic that detects colliding, it could be that the bounding boxes of the objects don't allow the two objects to ever come closer than 1. Because of that, the if-statement's condition never becomes true and therefore the code that stops your characters movement never runs.

For example

Imagine two perfect spheres (Fig-1). These represent the bounding boxes of two objects. In their centre is a point, this is their position. If they would somehow move in to each-other, the game engine would detect this colliding and revert the movement. This way, the actual distance between these two spheres could become equal to 0, but their cores will never get closer than 4. Some code that gets activated by the distance becoming less than 3 would thus never run.

Fig-1: Two objects with a circle as bounding box and a point as position: Two

The solution

The solution for your problem would than be to simply increase the minimum distance for the code to become triggered. Alternatively, you could write or implement a method that detects the actual distance between the two bounding boxes, but if these bounding boxes don't follow common shapes that could be quite a challenge.

Note

By adding Debug.Log("I should stop moving...") to the code inside the if-statement, you can check if it gets run.

\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .