0
\$\begingroup\$

So using Unity standard assets to get a stage rolling, Ive built a simple terrain and used FPSController as the player and AI ThirdPersonController (Ethan) as the enamy.

Ive built the navmesh and everything was working in its default state, but then I changed the AI script slightly as I wanted Ethan to follow me only if I come within a certain range, so I changed it to the following:

public float dangerRadius = 5f;

        private void Start()
        {
            // get the components on the object we need ( should not be null due to require component so no need to check )
            agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
            character = GetComponent<ThirdPersonCharacter>();

            agent.updateRotation = false;
            agent.updatePosition = true;
        }


        private void Update()
        {
            if (target != null)
                agent.SetDestination(target.position);

            if (agent.remainingDistance < dangerRadius)
                character.Move(agent.desiredVelocity, false, false);

            else
                character.Move(Vector3.zero, false, false);
        }


        public void SetTarget(Transform target)
        {
            this.target = target;
        }

But the problem is that when the remainingDistance is greater than dangerRadius or when I am out of range, Ethan just slides slowly towards the target in idle state and as the distance becomes lower than the threshold he starts running as expected.

Ive tried adding friction materials to the terrain and the collider on Ethan but he still slides.

Help thanks

\$\endgroup\$
4
  • \$\begingroup\$ What happens if you comment out the else block? \$\endgroup\$ Commented May 16, 2018 at 1:30
  • \$\begingroup\$ Still slides towards target :( \$\endgroup\$ Commented May 16, 2018 at 8:38
  • \$\begingroup\$ Have you tried to comment out both (the second if and its else) to see if the sliding remains? \$\endgroup\$
    – Nikaas
    Commented May 16, 2018 at 9:20
  • \$\begingroup\$ removing both if and else does not help, he is still sliding towards me \$\endgroup\$ Commented May 16, 2018 at 20:34

1 Answer 1

0
\$\begingroup\$

I think the transition from Idle animation to Running might be dependent on the velocity of the AI character. As in, the velocity has to be higher than a certain value and in your case it might not be hitting the mark when it's outside the danger radius. Can you check the parameter being used for the transition in the animator window?

\$\endgroup\$
2
  • \$\begingroup\$ Thanks. But it never transitions from idle to running, it just slides towards my direction in its idle state. It should not be moving at all. \$\endgroup\$ Commented May 17, 2018 at 15:06
  • \$\begingroup\$ Did you try stopping nav mesh agent in the else? I think it should be agent.Stop () \$\endgroup\$ Commented May 17, 2018 at 15:13

You must log in to answer this question.

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