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 thaiscorpion · Oct 21, 2013 at 04:21 PM · javascriptrandomitweenpathwaypoint

How to move an object on a random path with iTween

I'm trying to make a 'track' that is generated randomly as a object travels along that path.

For this I'm using iTween, the issue I'm having is that I can't seem to generate a random path of X nodes and animate the object on it.

iTween doesn't let me remove nodes and my only option is to move all of the nodes but this leads to jumps in the objects postion.

When the object goes past say 2 nodes I want to delete the older nodes and add new ones so the path will be 'infinite'.

Is there a way to make this happen? Any help would be great!

PD: I have seen this example http://itween.pixelplacement.com/examples.php#randomPaths but I can't figure out how this is done because I can't find the code.

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 robertbu · Oct 21, 2013 at 04:52 PM 0
Share

First, he sells the example packages. I believe $10 gets you the whole set. There is an "Add to Cart" link at the top of the page. Second, I may have an idea that would give you want you want, but why can't you just generate a whole new path each time your game object arrives at the end? Why do you need to modify the path as you go?

avatar image thaiscorpion · Oct 22, 2013 at 08:23 AM 0
Share

wouldn't it jump a bit or change speed when moving from one path to another?? When I tried to change path it didn't look very smooth. $$anonymous$$aybe I was implementing it wrong.

1 Reply

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

Answer by robertbu · Oct 22, 2013 at 04:25 PM

Here is a bit of code that recalculates a path when you reach the end. There are no speed issues moving from one path to the next. There is sometimes just a bit of roughness in the LookAt() during the transition (and this may be solvable). Note I'm using iTween.PointPonPath() and iTween.PutOnPath() for a bit more control. To test this code:

  • Create a large plane with position (0,-.5,0) and scale (100,100,100).

  • Put a texture on the plane so you can see movement

  • Add a cube at the origin

  • Add a directional light to see the cube and the plane

  • Add a Smooth Follow script to the camera with the cube as the target

  • Add the following script to the cube


    pragma strict

    var angle = 20.0; // Maximum angle offset for new point var speed = 8.0; // Units per second private var pos = 0.0; private var segLength = 2.0; private var path = new Vector3[100];

    function Start () { path[path.Length-2] = Vector3.zero; path[path.Length-1] = Vector3(0,0,segLength); RecalcPath(); }

    function Update () { iTween.PutOnPath(gameObject, path, pos); var v3 = iTween.PointOnPath(path, Mathf.Clamp01(pos+.01)); transform.LookAt(v3); pos = pos + Time.deltaTime * speed / path.Length; if (pos >= 1.0) { pos -= 1.0; RecalcPath(); } }

    function RecalcPath() { var v3 = path[path.Length-1] - path[path.Length-2]; path[0] = path[path.Length-1]; for (var i = 1; i < path.Length; i++) { var q = Quaternion.AngleAxis(Random.Range(-angle, angle), Vector3.up); v3 = q * v3; path[i] = path[i-1] + v3; } }

The code walks a cube on the XZ plane. It generates the path in equal length segments and uses 'angle' to generate a random direction within an angle of the previous segment at each point.

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 thaiscorpion · Oct 23, 2013 at 08:50 AM 0
Share

Robert that worked great. The only small issues is that the 'cube' keeps changing the orientation and looks a bit jagged so I just added a new cube that follows that one and controled its orientation seperatly and it looks perfect! Thank you for your help

avatar image robertbu · Oct 23, 2013 at 01:32 PM 0
Share

If you don't want the cube to change its orientation, comment out lines 17 and 18.

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

14 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

Related Questions

choose random waypoint 1 Answer

iTween Help Please MoveTo not working 1 Answer

iTween - Path Movement by Touch 0 Answers

How do you create random paths in iTween? 0 Answers

Generate random path in grid 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