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 maggot · Aug 09, 2010 at 07:53 PM · movementarraytimeloop

Movement through an array over time

This is my current code, attached to a gameobject. It causes the gameobject to go from y position 10 to 22 but I want it to do that and then from 0 to 22, then from -20.5 to 22 and finally 13 to 22. Also, I want destination to instead point to the next array element(with the usual restriction to the array length). Thanks!

private var enemy001yMovement = new Array(); enemy001yMovement[0] = 10; enemy001yMovement[1] = 0; enemy001yMovement[2] = -20.5; enemy001yMovement[3] = 13; var destination = 22;

function Update() { for(var pointer in enemy001yMovement) { transform.position = Vector3(0,Mathf.Lerp(pointer,destination,Time.time),0); } }

Comment
Add comment · Show 2
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 Ben 2 · Aug 09, 2010 at 09:56 PM 0
Share

You want to at least get rid of the for loop and maintain the index outside of Update. You want Update to act like the loop as it will run repeatedly over time.

avatar image maggot · Aug 10, 2010 at 05:27 PM 0
Share

Thanks, i think the answer is given more fully below

2 Replies

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

Answer by skovacs1 · Aug 09, 2010 at 10:24 PM

Like Ben said, you want to track the index into the array and as update is called each frame, it will take care of acting over time.

Something like this:

Unity js

private var yWaypoints = new Array(); yWaypoints[0] = 10; yWaypoints[1] = 0; yWaypoints[2] = -20.5; yWaypoints[3] = 13; var index = 1; //Index into the array at the destination

//Called each frame function Update() { //If we've gone through the array, we're done if(index < yWaypoints.length) { //Move from the previous position in the array to the next over time transform.position.y = Mathf.Lerp(yWaypoints[index-1],yWaypoints[index],Time.time);

     //If we've reached the destination, move to the next one
     if(transform.position.y == yWaypoints[index]) index++;
 }

}

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 maggot · Aug 10, 2010 at 05:21 PM 0
Share

Thanks a lot! Replacing the for loop with an if loop and using a .length array editor were the two differences i noticed first.

avatar image maggot · Aug 10, 2010 at 06:56 PM 0
Share

Eek, just tried it, it didnt work - got this error message : ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index 4

avatar image skovacs1 · Aug 12, 2010 at 02:02 PM 0
Share

Yeah, sorry about that. Typed it up in like 5 $$anonymous$$utes. Should have incremented last. Fixed now.

avatar image
1

Answer by Bampf · Aug 10, 2010 at 02:17 PM

It's worth taking the time to understand skovacs1's answer. If you don't understand when Update is called and how to use it, you won't get too far with Unity scripting.

For the specific problem of moving objects from place to place over time, which you will encounter over and over again, you might also want to look at iTween, which is free. The command to move a game object to position 0,22,0 over the next 2 seconds would look something like this: iTween.MoveTo(gameObject,Vector3(0,22,0),2);

You can queue up multiple movements, have the object start fast then slow down as it reaches the destination, and other effects. (Disclaimer: I'm planning to use it on my next project, and I expect it will save me a lot of time, but haven't have much hands-on with it yet.)

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 maggot · Aug 10, 2010 at 05:26 PM 0
Share

Yeh, of course - I find learning the correct placement of update function calling to be one of the more interesting parts of learning unity. I'm also new to javascript :) I've heard of itween, it looks useful and simple to implement but I wanted to learn how to do animations etc first

avatar image Bampf · Aug 11, 2010 at 03:01 AM 0
Share

Sounds good. Hope I didn't sound patronizing! I just wanted to add the additional info about iTween, without see$$anonymous$$g to suggest that iTween replaces the usefulness of understanding Update. Carry on! :-)

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

Large matrix formation and multiplication 0 Answers

Animator & movements 1 Answer

how do I scroll thru a array 2 Answers

while loop & array- turn on something via percentage 1 Answer

What's the best way to find the smallest Vector3.Distance of an array of enemies? 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