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 Jason Hamilton · Apr 24, 2011 at 04:43 PM · gameobjectarrayenemywaypointadd

add waypoint to transform[]

Hey, Im trying to add a new gameObject (empty) to a transform array thing (Im not to knowledgeable on how arrays work/what they are) and have a bot patrol between these empties, heres the code;

    var WayPoint : Transform[];
    private var CurrentWayPoint : int;
    var Speed : float = 20.0;

Awake() VVVVVVV

WayPoint[0] = transform;

Update() VVVVVVV

            if(CurrentWayPoint < WayPoint.length)
            {
                var target : Vector2 = WayPoint[CurrentWayPoint].position;
                var moveDirection : Vector2 = target - transform.position;
                var velocity = rigidbody.velocity;
                if(moveDirection.magnitude < 1)
                {
                    CurrentWayPoint++;
                }
                else
                {
                    velocity = moveDirection.normalized * Speed;
                }
            }
            else
            {
                if( loop )
                {
                    CurrentWayPoint = 0;
                }
                else
                {
                    velocity = Vector3.zero;
                }
            }
            rigidbody.velocity = velocity;

Now, this is the new waypoint;

var NewWayPoint = new GameObject("EnemyWayPoint3");
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by by0log1c · Apr 24, 2011 at 05:02 PM

I'm not 100% sure what you need but I figure you're asking how to add an element to a built-in array of predefined size. I wrote a simple example not long ago, here it is:

var myWPs:Transform[] = new Transform[9];
function AddWP(wp:Transform){
    var buffer:Transform[] = myWPs;
    myWPs = new Transform[buffer.length+1];
    for(i=0;i<buffer.length;i++){ myWPs[i] = buffer[i]; }
   myWPs[myWPs.length-1] = wp;
}
Comment
Add comment · Show 2 · 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 Joshua · Apr 24, 2011 at 05:04 PM 0
Share

Right but why use a built-in array anyway? It's not like this is a preformance critical piece of code. I'd just use new arr : Array = new Array (); and then arr = container.GetComponentsInChildren(Transform).

But maybe I'm just too lazy ^.^

avatar image Joshua · Apr 24, 2011 at 05:04 PM 0
Share

But still upvote for being correct :)

avatar image
0

Answer by Joshua · Apr 24, 2011 at 05:00 PM

I think this great tutorial might help you with exactly what you want. Go to step 5.

Now as to adding waypoint to an array. I assume that in your hierarchy view you have one empty gameobject (at 0,0,0) called waypoints. Childed to this empty container are all your waypoints. Have all these waypoints in alphabetical order as you want them to come up. For instance waypoint1, waypoint2, etc. Or just 1,2,3...

In your script use GetComponentsInChildren

nameofyourarray = nameofthewaypointscontainer.GetComponentsInChildren(Transform);

to add the transform of the container and all of its children (the waypoints) in that order in an array. Now you'll want to remove the first transform - the one of the useless container. Use Shift

nameofyourarray.Shift();

Now your array has all of the transforms of all your waypoints in the correct order. Reference them using

nameofyourarray[numberofthewaypoint].position

Good luck!

Comment
Add comment · Show 2 · 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 by0log1c · Apr 24, 2011 at 05:04 PM 0
Share

Note that this is using a Javascript Array, which is not declared using Type[]. Therefore I believe these functions will not work on the specific setting we're discussing.

avatar image Joshua · Apr 24, 2011 at 05:06 PM 0
Share

You're absolutely right that he'd have to change the part where he declares the variable into var WayPoint : Array = new Array ();

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

No one has followed this question yet.

Related Questions

Keep adding targets to a list 2 Answers

FindClosestEnemy() change target? 1 Answer

Way Point Assignment 4 Answers

instantiating objects problem 1 Answer

Get all other GameObjects (not the one script is attached to) 2 Answers


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