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