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 Tyrebear · Jan 31, 2014 at 05:30 AM · randomnavmeshwaypointrange

Can't set currentWaypoint integer with Random.Range

Hello I am working on a script that increments the currentWaypoint every time the player collides with a way point object. What I need to do now is set the currentWaypoint to random when the player reaches way point object 2. The problem is that because the variable currentWaypoint is not an array I cannot directly change it to a random value nor can I assign it to the waypoints variable because the waypoints variable is a transform type and not an integer. Here is my script.

 var waypoints : Transform[];
 var waypoint : Transform;
 var speed : int;
 var currentWaypoint : int;
 var agent : NavMeshAgent;
 
 
 function Update () {
 waypoint = waypoints[currentWaypoint]; // sets the active waypoint to the next currentwaypoint
 agent.SetDestination(waypoint.position); // go to the first waypoint
 }
 
 function OnTriggerEnter (other : Collider) {
 if (other.tag == "Waypoint" && waypoint == 2){
 waypoint = waypoints[Random.Range(0, waypoints.Length)];** //set currentWaypoint to random
 }
 else if  (other.tag == "Waypoint"){
 currentWaypoint++;
 }
 }

alt text

random waypoint.png (17.7 kB)
Comment
Add comment · Show 6
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 getyour411 · Jan 31, 2014 at 05:33 AM 0
Share

Check the tag on gameObject Waypoint #2. If it's properly tagged, add

 debug.log("Saw " + other.tag);

or something to validate you are getting the OnTriggerEnter event you are expecting.

When currentWaypoint is 4 and it hits the ++, bad things will happen.

avatar image Tyrebear · Jan 31, 2014 at 05:46 AM 0
Share

I have no problem moving the customer from one waypoint to another with a simple increment. Also I was planning on telling unity what to do after 4, later. Right now the problem is that the currentWaypoint variable wont randomize. If I put waypoint = waypoints[Random.Range(0, waypoints.Length)]; into the update function then the only thing that randomizes is the variable "waypoint" but it has no effect on the the "currentWaypoint" variable thus the customer will not move unless the "currentWaypoint" variable is changed.

avatar image getyour411 · Jan 31, 2014 at 05:55 AM 0
Share

You lost me there. I don't understand a few sentences in your question now. You can assign a random.range to an int.

"The problem is that because the variable currentWaypoint is not an array I cannot directly change it to a random value"

?

avatar image Tyrebear · Jan 31, 2014 at 06:11 AM 0
Share

Sorry for the confusion. If it is possible to assign a random.range to an integer how would I do it? If if I try currentWaypoint(Random.Range(0, waypoints.Length)); Unity says that it is not possible to invoke an expression of type 'int'.

avatar image Tyrebear · Jan 31, 2014 at 06:22 AM 0
Share

Thank you so much! You are a god hahaha. I can't believe it was so simple.

Show more comments

3 Replies

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

Answer by getyour411 · Jan 31, 2014 at 06:12 AM

currentWaypoint = Random.Range(....)

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 Tyrebear · Jan 31, 2014 at 08:58 AM

If anybody stumbles upon this thread then here is the source code.`var waypoints : Transform[]; var waypoint : Transform; var speed : int; var currentWaypoint : int; var agent : NavMeshAgent; var isMoving : boolean = false; var customer : GameObject;

function Update () { waypoint = waypoints[currentWaypoint]; if (isMoving == true){ // sets the active waypoint to the next currentwaypoint agent.SetDestination(waypoint.position); } }

function OnTriggerEnter (other : Collider) { if (other.tag == "Waypoint"){ currentWaypoint++; } if (currentWaypoint == 5){ Destroy(gameObject); } if (other.tag == "Waypoint" && currentWaypoint == 2){ currentWaypoint = Random.Range(2, 4); } }

function WaitAtPoint(){ isMoving = false; yield WaitForSeconds(5); }`

This will go to the way point objects that you set up in the scene and then select a random way point when you want it to. Make sure your map has a navigation map and make sure that the player you want to move has a navigation agent. I hope this helped someone like me and Have Fun! :D

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 Tyrebear · Jan 31, 2014 at 08:58 AM

 var waypoints : Transform[];
 var waypoint : Transform;
 var speed : int;
 var currentWaypoint : int;
 var agent : NavMeshAgent;
 var isMoving : boolean = false;
 var customer : GameObject;
 
 function Update () {
 waypoint = waypoints[currentWaypoint];
 if (isMoving == true){ // sets the active waypoint to the next currentwaypoint
 agent.SetDestination(waypoint.position);
 }
 }
 
 function OnTriggerEnter (other : Collider) {
 if (other.tag == "Waypoint"){
 currentWaypoint++;
 }
 if (currentWaypoint == 5){
     Destroy(gameObject);
     }
 if (other.tag == "Waypoint" && currentWaypoint == 2){
 currentWaypoint = Random.Range(2, 4);
     }
 }
 
 function WaitAtPoint(){
 isMoving = false;
 yield WaitForSeconds(5);
 }

This is the source code, hopefully it will help someone like me.

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

18 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

Related Questions

Walk to random waypoint 1 Answer

Random Range Seems... Unrandom 1 Answer

[SOLVED] NavMeshAgent Create Imperfect Paths? [SOLVED] 1 Answer

Store multiple random integers in an array? 4 Answers

[Beginner] Using Random.Range in initial game object position 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