- Home /
How to gradually increase the speed of a NavMesh agent?
So I have zombies that are clones of an original zombie and they all fallow the player, their speed right now (set in the inspector under NavMesh Agent) is 7.5. This is a good speed, however not for when the game first starts off. What I am asking/trying to do is make it so the zombie's start off with a speed of 1, then after 10 secs go to 1.5, then after 10 more secs go to 2, and so on and so on, then stop at 7.5 (for their maximum speed) at 130 seconds. How would I do this? I'm thinking writing the code in a C# script and then attaching it to the OG zombie. Any suggestions? Thanks, I do not know how to write this code.
Answer by abers1997 · Jul 26, 2017 at 06:24 AM
@ChompIV Constantly increasing a NavMeshAgent's speed is best done in a Coroutine just as screenname_taken suggested - Coroutine Unity Tutorial.
You'll want to loop your Coroutine in order for something to happen over and over again (in this case: every second). While loops are a good option. If you want to suspend the Coroutine execution and wait "amount of seconds" then - do something afterward; WaitForSeconds can be used to do just that.
Tested this and it gives the desired effect. Adds 0.5f to agent.speed every second and stops the Coroutine when agent.speed reached 7.5f. Agent will move to dest(C#):
UnityEngine.AI.NavMeshAgent agent;
//agent's destination
public Transform dest;
//max speed that can be attained
float speedCap = 7.5f;
void Start ()
{
//get NavMeshAgent component from gameObject that this script is attached to
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
//agent will move to "dest"
agent.destination = dest.position;
//begin increasing speed (StartCoroutine)
StartCoroutine("IncreaseSpeedPerSecond", 1f);
}
IEnumerator IncreaseSpeedPerSecond (float waitTime)
{
//while agent's speed is less than the speedCap
while (agent.speed < speedCap)
{
//wait "waitTime"
yield return new WaitForSeconds(waitTime);
//add 0.5f to currentSpeed every loop
agent.speed = agent.speed + 0.5f;
}
}
Feel free to ask me to elaborate on anything that isn't clear and I'll be glad to answer you to the best of my ability!
A side note: There are awesome tutorials related to NavMesh here and I highly recommend you check them out!
It doesn't seem to be working, where would I assign this script? I put it on the zombie that all the others clone off of, this is the script (btw thanks for the detailed help!)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ZedSpeed : $$anonymous$$onoBehaviour
{
UnityEngine.AI.Nav$$anonymous$$eshAgent agent;
//agent's destination
public Transform dest;
//max speed that can be attained
float speedCap = 7.5f;
void Start()
{
//get Nav$$anonymous$$eshAgent component from gameObject that this script is attached to
agent = GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent>();
//agent will move to "dest"
agent.destination = dest.position;
//begin increasing speed (StartCoroutine)
StartCoroutine("IncreaseSpeedPerSecond", 1f);
}
IEnumerator IncreaseSpeedPerSecond(float waitTime)
{
//while agent's speed is less than the speedCap
while (agent.speed < speedCap)
{
//wait "waitTime"
yield return new WaitForSeconds(waitTime);
//add 0.5f to currentSpeed every loop
agent.speed = agent.speed + 0.5f;
}
}
}
and do I put my fpscontroller in the dest spot? because they already fallow me
$$anonymous$$y pleasure, always glad to help!
It should work without issue for your zombie. However, I just tested it for clones and found that the clones have empty public variables once Instantiated. To fix that you'll need to make your zombie a prefab.
$$anonymous$$ake sure:
The script is attached to your zombie.
Your zombie is a prefab (drag from hierarchy to Project Folder).
Your zombie prefab's public slots are all plugged in with what corresponds to them in the Inspector.
Your Nav$$anonymous$$esh is properly baked (Window>Navigation>Bake).
Your player is a prefab and the player prefab is applied to "dest".
You can remove the dest system entirely if the zombies follow you already, you don't want two different scripts doing the same thing conflicting with each other. If that isn't the case, then put your plug in your player to "dest".
From what I understand, you have all the "follow" elements on another script. You'll need to incorporate this script to your already-working follow script (edited without "go to dest" element):
UnityEngine.AI.Nav$$anonymous$$eshAgent agent;
//max speed that can be attained
float speedCap = 7f;
void Start ()
{
//get Nav$$anonymous$$eshAgent component from gameObject that this script is attached to
agent = GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent>();
//begin increasing speed (StartCoroutine)
StartCoroutine("IncreaseSpeedPerSecond", 1f);
}
IEnumerator IncreaseSpeedPerSecond (float waitTime)
{
//while agent's speed is less than the speedCap
while (agent.speed < speedCap)
{
//wait "waitTime"
yield return new WaitForSeconds(waitTime);
//add 0.5f to currentSpeed every loop
agent.speed = agent.speed + 0.5f;
}
}
If things don't work out, post your "follow" script and your "instantiate zombie clones" scripts here.
hmmm, is'nt working but it's probably a fualty on my part, here is my new fallow script:
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class EnemyController : $$anonymous$$onoBehaviour {
Nav$$anonymous$$eshAgent nav;
Transform player;
Animator Controller;
UnityEngine.AI.Nav$$anonymous$$eshAgent agent;
float speedCap = 7.5f;
// Use this for initialization
void Awake () {
nav = GetComponent <Nav$$anonymous$$eshAgent> ();
player = GameObject.FindGameObjectWithTag ("Player").transform;
Controller = GetComponentInParent<Animator> ();
//get Nav$$anonymous$$eshAgent component from gameObject that this script is attached to
agent = GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent>();
//begin increasing speed (StartCoroutine)
StartCoroutine("IncreaseSpeedPerSecond", 1f);
}
// Update is called once per frame
void Update () {
nav.SetDestination (player.position);
Controller.SetFloat ("speed", $$anonymous$$athf.Abs (nav.velocity.x + nav.velocity.z));
}
IEnumerator IncreaseSpeedPerSecond(float waitTime)
{
//while agent's speed is less than the speedCap
while (agent.speed < speedCap)
{
//wait "waitTime"
yield return new WaitForSeconds(waitTime);
//add 0.5f to currentSpeed every loop
agent.speed = agent.speed + 0.5f;
}
}
}
and here is my cloning script:
using System.Collections;
using UnityEngine;
public class Game$$anonymous$$anagment : $$anonymous$$onoBehaviour {
public int round = 1;
float zombiesInRound = $$anonymous$$athf.Infinity;
int zombiesSpawnedInRound = 0;
float zombieSpawnTimer = 0;
public Transform[] zombieSpawnPoints;
public GameObject zombieEnemy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (zombiesSpawnedInRound < zombiesInRound)
{
if (zombieSpawnTimer > 2)
{
SpawnZombie ();
zombieSpawnTimer = 0;
}
else
{
zombieSpawnTimer += Time.deltaTime;
}
}
}
void SpawnZombie()
{
Vector3 randomSpawnPoint = zombieSpawnPoints [Random.Range (0, zombieSpawnPoints.Length)].position;
Instantiate(zombieEnemy, randomSpawnPoint, Quaternion.identity);
zombiesSpawnedInRound++;
}
}
Hey man your code worked fine but i have a question , even though i have assigned my player in the Dest i get this error why is that?
UnassignedReferenceException: The variable dest of DamagePlayerCollider has not been assigned.
You probably need to assign the dest variable of the DamagePlayerCollider script in the inspector.
UnityEngine.Transform.get_position ()
Answer by screenname_taken · Jul 25, 2017 at 08:20 AM
You just need to use NavMeshAgent.Speed. Increase it in a Coroutine every few seconds.
Your answer
Follow this Question
Related Questions
PhotonNetwork.IsClient, how to set tags for players? 2 Answers
NavMesh Modifier not changing navmesh 2 Answers
Navmesh agent forward movement 1 Answer
Need help detecting barriers for my game 0 Answers