• 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 Newman2393 · Nov 10, 2013 at 04:00 PM · waypointwaypoint system

How do I make my enemy stop at specific waypoints?

Hello everyone, I am making a waypoint system for a game, and it's working exactly the way I want it to. The only issue is that I want my enemy to stop at specific waypoints. So far I have only got it stopping at the very first waypoint. Can you please help me out?

 var accel = 0.8;
 var inertia = 0.9;
 var speedLimit = 10.0;
 var minSpeed = 1.0;
 var stopTime = 1.0;
 private var currentSpeed = 0.0;
 private var functionState = 0;
 private var moveState : boolean;
 private var waypoint : Transform;
 var rotationDamping = 6.0;
 var smoothRotation = true;
 var waypoints : Transform[];
 private var WPindexPointer : int;
 private var x : int;
 
 function Start ()
 {
     functionState = 0;
 }
 function Update ()
 {
     if (functionState == 0)
     {
         Accell ();    
     }
     if (functionState == 1)
     {
            Slow ();
     }
     waypoint = waypoints[WPindexPointer];
 }
 function Accell ()
 {
     if (moveState == false)
     {
         moveState = true;
     }
     if (waypoint)
     {
         if (smoothRotation)
         {
             var rotation = Quaternion.LookRotation(waypoint.position - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
         }
     }
     currentSpeed = currentSpeed + accel * accel;
     transform.Translate (0,0,Time.deltaTime * currentSpeed);
     if (currentSpeed >= speedLimit)
     {
         currentSpeed = speedLimit;
     }
 }
 function OnTriggerEnter (myTrigger : Collider)
 {
     switch(myTrigger.gameObject.name)
     {
         case "trigger1":
             stopTime = 5.0;
             Debug.Log(stopTime);
             functionState = 1;
             WPindexPointer++;
             break;
         case "trigger3":
             stopTime = 2.0;
             Debug.Log(stopTime);
             functionState = 1;
             WPindexPointer++;
             break;
         default:
             stopTime = 3.0;
             Debug.Log(stopTime);
             functionState = 1;
             WPindexPointer++;
             break;
     }
     if (WPindexPointer >= waypoints.Length)
     {
         WPindexPointer = 0;
     }
 }
 function Slow ()
 {
     Debug.Log("I am in Slow()");
     
     moveState = false;
     Debug.Log("moveState = false");
     currentSpeed = currentSpeed * inertia;
     transform.Translate (0,0,Time.deltaTime * currentSpeed);
     if (currentSpeed <= minSpeed)
     {
         currentSpeed = 0.0;
         Debug.Log("Current speed is 0");
         Debug.Log(stopTime);
         yield WaitForSeconds (stopTime);
         Debug.Log("Stopped");
         functionState = 0;
         Debug.Log("Off I go again");
     }
 }

Also, I figured out it's not running the if(currentSpeed <= minSpeed) function except on the first trigger.

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Why isn't a Ai Waypoint Spawning??? 0 Answers

Moving previous waypoint with Lerp 1 Answer

How will i make my objects/model(say a car) face the way points and travel towards it,instead of blindly following it? 2 Answers

How do I make a waypoint loop function using array? - when it gets to a waypoint, then move to next waypoint 1 Answer

How to make 2D multiple waypoints for sidescroller shooter or some other similiar movement 1 Answer


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