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
1
Question by Jerkenhag · Mar 08, 2013 at 02:35 PM · airandomwaypoint

How can I make an item choose between several different waypoints randomly and then make it execute something when it arrives to the waypoint?

The purpose of the game is to capture a golden ball which moves to different positions in the scene. I want to make about 8 waypoints over the scene which the ball can randomly choose one(with exception from the one it is placed on at that moment) and then go to a second waypoint, in 1 second lower down, wait there for 10 seconds and then take 1 sec to get up over the map and do the thing all over again. Preferably if it could take 3 seconds to move to next waypoint too so the whole operation takes 15 seconds. I have searched all day for an answer to this but cant find anything. Any explanation or link to something helpful would be great!

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

3 Replies

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

Answer by Statement · Mar 08, 2013 at 02:55 PM

  • Random.Range to get a random number to select one of the waypoints (if it gives the same waypoint that you currently are at, you could just try again).

  • Coroutines to make code work in sequence.

  • WaitForSeconds to wait for some time in the coroutine.

  • Vector3.MoveTowards or Vector3.Lerp to move the position in conjunction with transform.position.

Read the docs for each link provided and you should be able to puzzle it together. If you still are stuck, but have read the docs, please explain what confuses you and I'll try to help a bit further.

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
avatar image
1

Answer by poncho · Mar 08, 2013 at 02:51 PM

enumerate the waypoints, then use a random like Random.Range(0,8), get your random waypoint index, use that waypoint, getyourself a time dependant variable, float myTimeDependantVariable, that gets the time from the update Time.deltaTime, add states, countingto1, countingto10, countingto3, change states according to what you need. hope you get it, happy coding

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
avatar image
0

Answer by Jerkenhag · Mar 08, 2013 at 06:52 PM

Thanks for the answers! I have read through the documents and searched at some other places to find out how to lower the object for 10 seconds. However I cant seem to make the object move to next waypoint with this code but when I have the object not lowered it manages to move back and forth between the waypoints but not with the time of 3 seconds it just pops between the points. This is not the major concern so if I just could get some tip about how to make this code go to start again that would probably be just fine!

 var waypoint : int;
 var currentWaypoint : int;
 
 var start : Transform;
 var end;
 
 waypoint = 1;
 currentWaypoint = 1;
 
 var waypoint1 : Transform;
 var waypoint2 : Transform;
 var waypoint3 : Transform;
 var waypoint4 : Transform;
 
 function Start(){
 
     currentWaypoint = Random.Range(1, 4);
         
     if(currentWaypoint != waypoint){
         
         if(currentWaypoint == 1){
                         
             transform.position = Vector3.Lerp(start.position, waypoint1.position, 3);
             
             yield WaitForSeconds(1);
             
             transform.Translate(Vector3.up * -10, Space.Self);
             
             yield WaitForSeconds(10);
             
             transform.Translate(Vector3.up * 10, Space.Self);
             
             yield WaitForSeconds(1);
             
             waypoint = currentWaypoint;
             
         }
         
         else if(currentWaypoint == 2){
                             
             transform.position = Vector3.Lerp(start.position, waypoint2.position, 3);
             
             yield WaitForSeconds(1);
             
             transform.Translate(Vector3.up * -10, Space.Self);
             
             yield WaitForSeconds(10);
             
             transform.Translate(Vector3.up * 10, Space.Self);
             
             yield WaitForSeconds(1);
             
             waypoint = currentWaypoint;
             
         }
         
         else if(currentWaypoint == 3){
                             
             transform.position = Vector3.Lerp(start.position, waypoint3.position, 3);
             
             yield WaitForSeconds(1);
             
             transform.Translate(Vector3.up * -10, Space.Self);
             
             yield WaitForSeconds(10);
             
             transform.Translate(Vector3.up * 10, Space.Self);
             
             yield WaitForSeconds(1);
             
             waypoint = currentWaypoint;
             
         }
         
         else if(currentWaypoint == 4){
                             
             transform.position = Vector3.Lerp(start.position, waypoint4.position, 3);
             
             yield WaitForSeconds(1);
             
             transform.Translate(Vector3.up * -10, Space.Self);
             
             yield WaitForSeconds(10);
             
             transform.Translate(Vector3.up * 10, Space.Self);
             
             yield WaitForSeconds(1);
             
             waypoint = currentWaypoint;
             
         }
     }
     
     else if(currentWaypoint == waypoint){
     
         currentWaypoint = Random.Range(1, 4);
     
     }
     
 }
Comment
Add comment · Show 1 · 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 Jerkenhag · Mar 08, 2013 at 08:58 PM 0
Share

I made it work by adding:

 while(currentWaypoint<5){
 
     "code"
 
 }

so that it would go on forever. Thank you for your tips!

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

12 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

Related Questions

Random Running and Follow at Certain Distance 0 Answers

How can I make AI follow waypoints and choose random ones? 1 Answer

AI Pathfinding 1 Answer

Wandering enemies 1 Answer

Confused about copying Lists! 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