NavAgent doesn't seem to turn to face destination

Hi all

I posted this in the Navigation forum a while ago but haven’t gotten any answers so I’d thought I would try here.

I am using Gaia to create a mountain terrain and have a NavMesh created for it as well as an NaveMeshAgent.

I am using the following code to control the movement of the NavMeshAgent, as well as Opsive’s Behaviour Designer.

The NavMeshAgent seems to be following the pathway set out by behavior tree but when the NavMeshAgent turns to go to another destination point it doesn’t rotate to face the new direction.

I originally used Unity’s terrain to create a flat terrain with the same behavior tree and NavMeshAgent but didn’t notice this behavior at all.

Here’s the code:

1.    using UnityEngine;


2.    using System.Collections;


3.    using BehaviorDesigner.Runtime;


4.   


5.   


6.   


7.    public class MovmentScript : MonoBehaviour


8.    {


9.        //link to Animator component


10.        public Animator animController;


11.        //used to set anim controller parameters


12.        public enum MoveState { Idle, Walking, Run, Attack}


13.        public MoveState moveState;


14.        //link to NavMeshAgent component


15.        public UnityEngine.AI.NavMeshAgent navAgent;


16.        public BehaviorTree behaviorTree;


17.        private bool canSeePlayer;


18.        private bool canAttack;


19.        public float damageAmount;


20.   


21.   


22.        public void Start()


23.     


24.        {


25.            canSeePlayer = ((SharedBool)behaviorTree.GetVariable("Chase")).Value;


26.            canAttack = ((SharedBool)behaviorTree.GetVariable("Attack")).Value;


27.         


28.   


29.         


30.       


31.        }


32.     


33.        // Update is called once per frame


34.        void Update()


35.        {


36.            //character walks if there is a navigation path set, idle all other times


37.            canSeePlayer = ((SharedBool)behaviorTree.GetVariable("Chase")).Value;


38.            canAttack = ((SharedBool)behaviorTree.GetVariable("Attack")).Value;


39.   


40.   


41.   


42.            if (canSeePlayer)


43.   


44.            {


45.                moveState = MoveState.Run;


46.                //print(moveState);


47.                print("I see you!");


48.   


49.                if (canAttack)


50.                {


51.                    //print("Attacking you!");


52.                    vp_LocalPlayer.Damage(damageAmount);


53.                    moveState = MoveState.Attack;


54.   


55.                }


56.   


57.   


58.            }


59.            else


60.            {


61.                moveState = MoveState.Walking;


62.            }


63.   


64.         


65.   


66.         


67.            //send move state info to animator controller


68.            animController.SetInteger("MoveState", (int)moveState);


69.   


70.        }


71.        void OnAnimatorMove()


72.        {


73.            //only perform if walking


74.            if (moveState == MoveState.Walking)


75.            {


76.                //set the navAgent's velocity to the velocity of the animation clip currently playing


77.                navAgent.velocity = animController.deltaPosition / Time.deltaTime;


78.   


79.                //smoothly rotate the character in the desired direction of motion


80.                Quaternion lookRotation = Quaternion.LookRotation(navAgent.desiredVelocity);


81.                transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, navAgent.angularSpeed * Time.deltaTime);


82.            }


83.        }


84.     


85.     


86.    }


87.   


88.

Thanks

Sorry for the code not being formatter correctly

Instead of editing the rotation like that, Wouldn’t Transform.LookAt work?

Is the agent.UpdateRotation set to false? If it isn’t your code won’t work as the agent will override it.

Okay, thanks all. I will try the Transform.LookAt as well as the agent.UpdateRotation.

This code was adapted from a MecWarriors code snippet that was posted on the Behaviour Designer behavior tree forum.

I have added the agent.UpdateRotation=false to the OnAnimatorMove() callback function but it doesn’t seem to help. The agent still is facing forward when moving to another location.
I am using Gaia for the terrain, UFPS, and Behaviour Designer for the agent’s navigation.
I am not sure what is going on.
I have attached a video to show the problem I am having.

Well I found the problem!
I was playing around with the behavour tree and decided to check the Wander task. I wanted to see if there may be something in the task’s variables that was affecting how the agent was moving, particularly the rotation.

I decided to delete the Wander task and try a Search task. This task only has the agent go in one direction at a time it seems. So I went back to the Wander task wanting to change the rotation parameter. After adding back the Wander task, the script was working as it should.

I am not sure why this should have happened, as I am not too familiar with BT. But, I have it working. I will mention this in the Opsive forums and see if they have an explanation.