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 Major · Aug 10, 2012 at 12:22 AM · transformarrayenemyspawnwaypoint

Way Point Assignment

This topic has been asked a couple of times, but none of them i understand very well. The problem is i want to spawn my enemies, then have them follow way points. But sense i can't assign them in the project panel i have to do i by script. i will post the script that i am using (JavaScript). thanks!

 var waypoint : Transform[];
 var speed : float = 5;
 
 private var currentWaypoint : int;
 
     function Update () 
     { 
         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;
             }
         }
 
         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

4 Replies

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

Answer by Brett_MMU · Aug 10, 2012 at 06:10 PM

If you need them in order then you'll have to do something to each waypoint to make it identifiable. If you don't have many waypoints the easiest thing to do would be to tag each waypoint as waypoint1, waypoint2, waypoint3 etc, then have

 var waypoint: GameObject[];

 Start() 
 { 
     waypoint = new GameObject[numberOfWaypoints]; 
     waypoint[0] = GameObject.FindGameObjectWithTag("waypoint1); 
     waypoint[1] = GameObject.FindGameObjectWithTag("waypoint2); 
     waypoint[2] = GameObject.FindGameObjectWithTag("waypoint3); 
 }

or you could attach a script to each waypoint with a public int set to it's ID;

then have (this is in C#, not sure if it's the same for JavaScript)

 GameObject[] waypoints; 
 GameObject[] sortedWaypoints;

 Start() 
 { 
     waypoints = GameObject.FindGameObjectsWithTag("waypointTag");
     sortedWaypoints = new GameObject[waypoints.Length()]; 
     foreach (GameObject g in waypoints) 
     { 
         sortedWaypoints[g.GetComponent<waypointScript>().ID] = g; 
     } 
 }

if you did it this way you're first waypoint must have an ID of 0, then the second waypoint has an ID of 1 and so on

Comment
Add comment · Show 4 · 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 Major · Aug 10, 2012 at 06:27 PM 0
Share

This is the only way to order them?

avatar image Brett_MMU · Aug 10, 2012 at 06:31 PM 0
Share

that I can think of, can you not select the script in the project pane and drag the waypoint transforms into the inspector?

avatar image Major · Aug 10, 2012 at 06:40 PM 0
Share

i can do that i just wanted to explore my options a little. :)

avatar image Major · Aug 10, 2012 at 06:45 PM 0
Share

I tried the second way (it would be much easier because i have 17 waypoints), and i don't think it works for javascript. i got 6 errors. :(

avatar image
0

Answer by Brett_MMU · Aug 10, 2012 at 02:04 AM

what I do is tag my waypoints, then add a start function like this (it's in C# but I imagine it's the same for JavaScript)

 Start()
 {
     waypoints = GameObject.FindGameObjectsWithTag("waypointTag");
 }

If you do it this way though you'll have to change waypoints to a GameObject instead of a transform, and then use waypoint[currentwaypoint].transform.position

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 Major · Aug 10, 2012 at 05:24 AM 0
Share

where would i add the waypoint[current waypoint].transform.position?

avatar image Brett_MMU · Aug 10, 2012 at 09:44 AM 0
Share

replace

var target : Vector3 = waypoint[currentWaypoint].position;

with

var target : Vector3 = waypoint[currentWaypoint].transform.position;

avatar image Major · Aug 10, 2012 at 05:41 PM 0
Share

alight i did what you said, and it works more reliably now, but when it finds the gameobjects with the tag waypoint, it puts them in a random order inside the array.

avatar image
0

Answer by Brett_MMU · Aug 10, 2012 at 06:50 PM

 var waypoints : GameObject[]; 
 var sortedWaypoints : GameObject[];

 function Start() 
 { 
     waypoints = GameObject.FindGameObjectsWithTag("waypointTag");
     sortedWaypoints = new GameObject[waypoints.length]; 
     for (g in waypoints) 
     { 
         sortedWaypoints[g.GetComponent(waypointScript).ID] = g; 
     } 
 }

try that

Comment
Add comment · Show 13 · 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 Major · Aug 10, 2012 at 06:58 PM 0
Share

okay no more errors, but what do you mean bu id in the first answer? is that the name or something else? i named all of the waypoints 0-16 in case that is what you meant.

avatar image Brett_MMU · Aug 10, 2012 at 07:02 PM 0
Share

create another javascritpt called waypointScript and just add

public var ID : int;

then attach it to every waypoint, and set the ID in the inspector

avatar image Major · Aug 10, 2012 at 07:12 PM 0
Share

i got a null referance at this line:

sortedWaypoint = new GameObject[Waypoint.Length];

avatar image Brett_MMU · Aug 10, 2012 at 07:18 PM 0
Share

does your waypoint array have an upper case W? if not try it with a lower case w

sortedWaypoint = new GameObject[waypoint.length];

avatar image Major · Aug 10, 2012 at 07:25 PM 0
Share

okay i did lowercase that got rid of it, but they still don't follow the way points in order of the id i gave them on the script.

Show more comments
avatar image
0

Answer by Brett_MMU · Aug 10, 2012 at 08:01 PM

Add

Debug.Log(currentWaypoint); Debug.Log(sortedWaypoint[0].transform.position);

after that line and tell me what you get

Comment
Add comment · Show 4 · 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 Major · Aug 10, 2012 at 09:01 PM 0
Share

sorry for the latness but i got 2 things:

  1. UnityEngine.Debug:Log(Object)

2.(36.7, 2.5, 48.6)UnityEngine.Debug:Log(Object)

avatar image Brett_MMU · Aug 10, 2012 at 09:08 PM 0
Share

the only thing I can think of that would be throwing a null reference exception is that your ID's aren't correct, a typo maybe? like 0,1,3,3,4 or something like that

is it constantly been thrown?

avatar image Major · Aug 10, 2012 at 09:20 PM 0
Share

it works now. for some reason the ids of all the way points reverted to 0 even though i set them. :) Thanks a million!

avatar image Brett_MMU · Aug 10, 2012 at 09:24 PM 0
Share

no problem, good luck with your game

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

8 People are following this question.

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

Related Questions

What is wrong with this code? 0 Answers

add waypoint to transform[] 2 Answers

Enemies Spawn, Then Follow Waypoints 1 Answer

problem extracting transform from an array 1 Answer

Spawn "n" Enemies Every "nth" Wave? 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