Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MKO · May 09, 2012 at 03:33 PM · waypointwaypointswaypoint system

waypoint system help

Hi, i've been having trouble figuring out how to add a way for the rigid-body to stop at a specific waypoint then continue on to the next waypoint.

here is the code so far.

 var WalkSpeed = 1.0;
 var speed : float = 5.0;
 private var currentWaypoint : int;
 var loop = true;
 var waypoint : Transform[];

 function Start(){
 animation["walk"].wrapMode = WrapMode.Loop;
 
 }
 
 function Update(){
     
     animation["walk"].speed = WalkSpeed;
     
     if(currentWaypoint < waypoint.length){
         
         var target : Vector3 = waypoint[currentWaypoint].position;
         var moveDirection : Vector3 = target - transform.position;
         var velocity = rigidbody.velocity;
         
         if(moveDirection.magnitude < 1){
             currentWaypoint++;
             
         }
         else{
             velocity = moveDirection.normalized*speed;
             animation.Play("walk");
         }
     }
     
     else{
         if(loop){
             currentWaypoint = 0;
                 
         }
         else{
             velocity = Vector3.zero;    
         }
         
     }
     
     
     rigidbody.velocity = velocity;
     transform.LookAt(target);
 }
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

1 Reply

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

Answer by sestren · May 09, 2012 at 05:22 PM

What exactly is the problem? At a quick glance this code should work for moving between waypoints. Do you simply need the object to stop as it approaches a waypoint and then continue on after a set period of time?

If that's the case then you can just add a check in this section:

 if(moveDirection.magnitude < 1){
             currentWaypoint++;
 }

Before you increment the currentWaypoint value simply set a value to the current Time.time. Afterwords check to see if the difference between the current Time.time and your base start value exceeds your pause duration. If it does, continue on to the next waypoint.

 if(moveDirection.magnitude < 1){
             if (curTime == 0)
                  curTime = Time.time;
             if ((Time.time - curTime) >= pauseDuration) {
                  currentWaypoint++;
                  curTime = 0;
             }
             else {
                  velocity = Vector3.zero;
             }
 }

Setting the velocity straight to zero is obviously not the best way to handle a "pause", but it works for the sake of this example. You would probably want to smoothly move the value down toward zero depending on the situation (and consequently, push up the waypoint detection range from 1 to something higher).

Comment
Add comment · Show 3 · 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 MKO · May 09, 2012 at 07:54 PM 0
Share

Ah, that almost what was ai$$anonymous$$g for but not exactly.(sorry for not being specific enough)

let me try to convey better what i had in $$anonymous$$d: i was wondering about storing specific waypoints so that the object would stop at those specific ones, otherwise it would just continue moving along the given path.

thank you for your help.

avatar image sestren · May 09, 2012 at 09:09 PM 0
Share

Same idea. You would check in that same IF block if the 'currentWaypoint' matches any value in a list of pause waypoints. How you do that depends on how you want the pauses to work... If you want it to stop at EVERY saved location you would iterate over the list and compare each value to the current waypoint to see if it matches. If you wanted to compare it only to it's exact counterpart in the corresponding list then you could do it without looping and just using the index. If the waypoint values match just set the velocity to zero (or gradually decline) as I did in the example above.

avatar image fafase · May 10, 2012 at 05:21 AM 0
Share

the first if checks the distance, the second checks if curTime is 0, if not it sets it to Time.time. Then it checks if Time.time-curTime (rightnow this is 0)>pauseDuration so passed the if and do the else. velocity is assigned Vector3.zero. ok, but next round does exactly the same since I do not see where curTime is changed.

 var curTime:float;
 if(moveDirection.magnitude < 1){
         curTime= Time.deltaTime;
         if (curTime >= pauseDuration) {
              currentWaypoint++;
              assignementOfNewTarget();
              curTime = 0;
         }
         
         // This part not so useful as this section does not have any moving code.
         else {
              velocity = Vector3.zero;
         }

}

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

6 People are following this question.

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

Related Questions

How do you move a character to another waypoint after landing on a specific waypoint 1 Answer

Making users able to make custom tracks to follow by the character in the game 0 Answers

Getting script to update list of transforms when destination reached or the transform's game object is inactive 0 Answers

Easy WayPoint System - Character climbing at waypoint 0 Answers

How to play an animation with waypoints 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