Offset NavMeshAgent Destination
I would like to code my NavMeshAgent to move at a diagonal relative to its destination, all while speeding up.
I've tried several different approaches, but in every case the agent moves in a straight line toward its destination. My latest effort is posted below. NB is the NavMeshAgent, and for context I've left in my comments on old snippets that don't work.
public NBStatePatternEnemy enemy;
public Animator anim;
private Vector3 desPos;
private Vector3 tarPos;
public Vector3 skitterOffset = new Vector3 (15, 0, 0);
private int nextSkitterPoint;
void Awake ()
{
enemy = GetComponent<NBStatePatternEnemy>();
anim = GetComponent<Animator>();
}
void Start ()
{
StartCoroutine (SkitterLogic());
}
// Update is called once per frame
void Update()
{
}
IEnumerator SkitterLogic()
{
float minWaitTime = 2.0f;
float maxWaitTime = 5.0f;
while (enemy.isChasing && enemy.isGrounded)
{
Debug.Log ("SkitterLogic active.");
yield return new WaitForSeconds(Random.Range(minWaitTime, maxWaitTime));
Skitter ();
}
}
void Skitter()
{
/*if (enemy.nachoBeastJump == enabled)
{
enemy.nachoBeastJump.enabled = false;
} */
//MAYBE SETTING DESTINATION IS NOT THE WAY TO GO?
//enemy.navMeshAgent.destination = enemy.chaseTarget.position + Vector3.right * skitterOffset;
//THIS ACTUALLY MOVES THE PLAYER. LOL.
//enemy.chaseTarget.position += enemy.skitterPoint;
//CLOSE. THIS IS THE RIGHT METHOD TO WORK IN, BUT THE BELOW STILL DOESN'T OVERRIDE THE NB DESTINATION
//enemy.navMeshAgent.Move (skitterOffset * Time.deltaTime);
//FLOATS IN AN ARRAY CAN'T BE ADDED TO THE RED X AXIS. HO HUM.
//nextSkitterPoint = Random.Range (0, enemy.skitterOffsets.Length);
desPos = enemy.navMeshAgent.destination;
enemy.navMeshAgent.destination = desPos;
tarPos = enemy.chaseTarget.position;
enemy.chaseTarget.position = tarPos;
enemy.navMeshAgent.Stop ();
enemy.meshRendererFlag.material.color = Color.red;
desPos.x = tarPos.x += enemy.skitterOffset;
//enemy.navMeshAgent.destination = enemy.chaseTarget.position;
enemy.navMeshAgent.Resume();
anim.SetTrigger ("Skitter Initiated");
enemy.SetSkittering (true);
enemy.navMeshAgent.acceleration = 60.0f;
enemy.navMeshAgent.speed = 20.0f;
}
Code TL;DR: I'm trying to make the Vector3's of the agent and destination writable, and then adding a float to achieve the diagonal. I know it's not the right approach and I'd like to know what is.
Your answer
Follow this Question
Related Questions
Movement causing infinite loops in OnTriggerEnter and OnTriggerExit. 1 Answer
Need some example C# Syntax 0 Answers
Why does my Nav Mesh Agent walk to the wrong side of its destination object? 0 Answers
Occasional NavMeshAgent gets stuck and goes the wrong direction 1 Answer
Using Rigidbody physics to move an enemy with NavMeshAgent - De-sync issues 0 Answers