Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ChompIV · Jul 25, 2017 at 07:06 AM · c#navmeshnavmeshagentwaitforsecondstime.deltatime

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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!

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ChompIV · Jul 26, 2017 at 06:29 PM 0
Share

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;
         }
     }
 }
avatar image ChompIV · Jul 26, 2017 at 06:29 PM 0
Share

and do I put my fpscontroller in the dest spot? because they already fallow me

avatar image abers1997 ChompIV · Jul 27, 2017 at 03:12 AM 0
Share

$$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.

avatar image ChompIV abers1997 · Jul 27, 2017 at 06:59 AM 0
Share

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++;
     }
 }


Show more comments
avatar image TheUninvited · Oct 31, 2018 at 07:52 PM 0
Share

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 ()


avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

362 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

NavAgent not following on rebuilt NavMesh 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges