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 /
  • Help Room /
avatar image
0
Question by Dylanpolis · Feb 22, 2017 at 04:48 AM · scripting problemscripting beginnerscriptingbasics

Scripting help?

 // Patrol.cs
 using UnityEngine;
 using System.Collections;
 
 
 public class Patrol : MonoBehaviour {
 
     public Transform[] points;
     private int destPoint = 0;
     private NavMeshAgent agent;
 
 
     void Start () {
         agent = GetComponent<NavMeshAgent>();
 
         // Disabling auto-braking allows for continuous movement
         // between points (ie, the agent doesn't slow down as it
         // approaches a destination point).
         agent.autoBraking = false;
 
         GotoNextPoint();
     }
 
 
     void GotoNextPoint() {
         // Returns if no points have been set up
         if (points.Length == 0)
             return;
 
         // Set the agent to go to the currently selected destination.
         agent.destination = points[destPoint].position;
 
         // Choose the next point in the array as the destination,
         // cycling to the start if necessary.
         destPoint = (destPoint + 1) % points.Length;
     }
 
 
     void Update () {
         // Choose the next destination point when the agent gets
         // close to the current one.
         if (agent.remainingDistance < 0.5f)
             GotoNextPoint();
     }
 }




I would like to have the character to stop at each point for x seconds then move to the next point and do so again for each point. Is there any way i can do this without coroutines? I am not experienced enough to do coroutines so im trying to see if there is any other way.

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
0
Best Answer

Answer by oStaiko · Feb 22, 2017 at 06:33 AM

It seems there's been a lot of posts like this where people forgot that the UnityEngine.Time class exists. Not only that, but there's also System.DateTime, and even System.Diagnostics.StopWatch if you're despereate. This is not what what Coroutines were meant for, if you need to count things, just use Time.time. It counts the seconds as a float from the moment the scene (or maybe game?) loaded. Regardless, it can be treated like any old float. By setting a time in the future, like Time.time + 5, we can use an if statement to see when Time.time catches up to that number, which will occur the first frame from 5 seconds after that was set.

 // Patrol.cs
 using UnityEngine;
 using System.Collections;
  
  
 public class Patrol : MonoBehaviour
 {
     public Transform[] points;
     privatefloat waitTime = 0;
     private int destPoint = 0;
     private NavMeshAgent agent;
 
     void Start ()
     {
         agent = GetComponent<NavMeshAgent> ();
         agent.autoBraking = false;
         GotoNextPoint();
     }
 
     void GotoNextPoint ()
     {
         waitTime = 0;
         if (points.Length == 0)
             return;
         agent.destination = points[destPoint].position;
         destPoint = (destPoint + 1) % points.Length;
     }
 
     void Update ()
     {
         if (agent.remainingDistance < 0.5f && waitTime == 0)
             waitTime = Time.time + 5; //seconds goes here
         else if (agent.remainingDistance < 0.5f && Time.time > waitTime)
             GotoNextPoint();
     }
 }

Logic is simple. If we're in range but not timing yet, start timing. Else if were in range and we are past our timer, reset the time and move to new point.

Comment
Add comment · Show 2 · 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 Dylanpolis · Feb 23, 2017 at 12:46 AM 0
Share

when the enemy gets to each point she starts to sway from left to right then moves to next point. Could it be another script causing this?

avatar image oStaiko Dylanpolis · Feb 23, 2017 at 06:15 AM 0
Share

It is probably another script yet, This script would not cause them to move during the wait time.

avatar image
0

Answer by Dylanpolis · Feb 23, 2017 at 12:35 AM

Thank you for the help! I did not forget by the way. Im just new to unity.

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

121 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

Related Questions

Timer wont work 0 Answers

Getting weird error message!?!?!? 1 Answer

Make enemy patrol without Nav Points? 0 Answers

tagged objects problem 0 Answers

random spawn enemy problem 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