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\$
  • \$\begingroup\$ What happens if you comment out the else block? \$\endgroup\$ – Draco18s May 16 '18 at 1:30
  • \$\begingroup\$ Still slides towards target :( \$\endgroup\$ – StuckInPhD May 16 '18 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 May 16 '18 at 9:20
  • \$\begingroup\$ removing both if and else does not help, he is still sliding towards me \$\endgroup\$ – StuckInPhD May 16 '18 at 20:34
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\$
  • \$\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\$ – StuckInPhD May 17 '18 at 15:06
  • \$\begingroup\$ Did you try stopping nav mesh agent in the else? I think it should be agent.Stop () \$\endgroup\$ – Vishal Mohan May 17 '18 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.