Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
24
Question by mdaustin · Mar 06, 2010 at 11:00 PM · transformbezier

Moving an Object along a Bezier Curve

I have the following code which is attempting to move an object along a path of 3 defined points using my interpretation (which is most likely the problem) of a Bezier curve. I am aiming longterm to change the EndPoint variables to the StartPoint variables to start a new curve from the end of this one.

I have solved the problem I had and this code now works for anyone who wants the same kind of effect.

 var StartPointX: float = 0;
 var StartPointY: float = 0;
 var ControlPointX: float = 20;
 var ControlPointY: float = 50;
 var EndPointX : float = 50;
 var EndPointY : float = 0;
 var CurveX:float;
 var CurveY: float;
 var BezierTime: float = 0;
 var mySphere: Transform;
 
 function Update()
 {
     BezierTime = BezierTime + Time.deltaTime;
 
     if (BezierTime >= 1)
     {
         BezierTime = 0;
     }
 
     CurveX = (((1-BezierTime)*(1-BezierTime)) * StartPointX) + (2 * BezierTime * (1 - BezierTime) * ControlPointX) + ((BezierTime * BezierTime) * EndPointX);
     CurveY = (((1-BezierTime)*(1-BezierTime)) * StartPointY) + (2 * BezierTime * (1 - BezierTime) * ControlPointY) + ((BezierTime * BezierTime) * EndPointY);
     transform.position = Vector3(CurveX, CurveY, 0);
 
 }
Comment
Add comment · Show 22
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 mdaustin · Mar 06, 2010 at 11:13 PM 1
Share

I have edited to the above question to show a now working solution

avatar image pixelboi · Dec 21, 2010 at 10:25 PM 0
Share

Do I just add this to a cube or sphere to text? What do I use for the transform? I tried adding the script to an empty game object. Then I draged the cube to the "Transform" zeroed the "Y' values. I get nothing. Help please.

Thanks

avatar image Hiten2012 pixelboi · Jul 24, 2015 at 10:42 AM 0
Share

@dentedpixel Do we needs to attach any external file to use LeanTween? because it seems that its not in unity api.

avatar image ATMEthan pixelboi · Jul 24, 2015 at 12:44 PM 1
Share

@Hiten2012 its a unity asset store package that you have to download - https://www.assetstore.unity3d.com/en/#!/content/3595 EDIT: to reflect its a free downloand and no purchases required.

avatar image dentedpixel pixelboi · Jul 24, 2015 at 12:54 PM 1
Share

Correct, it's a free open source project though, so you just really have to download it (not really a purchase).

Show more comments
avatar image pickledzebra · Jan 12, 2011 at 11:59 PM 0
Share

So helpful!!! Is the 3D version simple?

avatar image zannghast · Feb 01, 2011 at 06:12 AM 0
Share

CurveZ = (((1-BezierTime)(1-BezierTime)) StartPointZ) + (2 BezierTime (1 - BezierTime) ControlPointZ) + ((BezierTime BezierTime) * EndPointZ); << - add this and you'll have the 3D version. Of course, you'll have to declare the necessary variables for Z.

avatar image Marvin Hawkins · Feb 18, 2011 at 12:54 PM 0
Share

This is a great solution. But I have to ask, how do you control the speed variable? by default, the object is way too fast. Thanks again!

Show more comments

3 Replies

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

Answer by mdaustin · Mar 06, 2010 at 11:15 PM

The original question was a mistyping of the actual equation. I have changed the code so that it is now workable for anyone whom wants to adapt or use it.

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
8

Answer by Bunny83 · Jul 25, 2011 at 06:40 PM

Well, just found this question, i know you got it to work but you've heard about Vector2 / Vector3 and functions?

 function Bezier2(Start : Vector2, Control : Vector2, End : Vector2 , t :float) : Vector2
 {
     return (((1-t)*(1-t)) * Start) + (2 * t * (1 - t) * Control) + ((t * t) * End);
 }
 
 function Bezier2(Start : Vector3, Control : Vector3, End : Vector3 , t :float) : Vector3
 {
     return (((1-t)*(1-t)) * Start) + (2 * t * (1 - t) * Control) + ((t * t) * End);
 }
 
 function Bezier3(s : Vector2, st : Vector2, et : Vector2, e : Vector2, t : float) : Vector2
 {
     return (((-s + 3*(st-et) + e)* t + (3*(s+et) - 6*st))* t + 3*(st-s))* t + s;
 }
 
 function Bezier3(s : Vector3, st : Vector3, et : Vector3, e : Vector3, t : float) : Vector3
 {
     return (((-s + 3*(st-et) + e)* t + (3*(s+et) - 6*st))* t + 3*(st-s))* t + s;
 }

That's are two versions: a quadratic bezier(one control point) and a cubic bezier(two control points). Each comes in two versions: Vector2 and Vector3.

The cubic equatation is transformed so you don't have those ugly (1-t)(1-t)(1-t). When you just multiply the brackets you get (1 + (-3 +(3-t)*t)*t) which looks even more ugly but with the other terms it resolves quite nicely ;)

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 flamy · Dec 23, 2011 at 03:56 PM 1
Share

thanks a lot for this solution, it was really helpful.

avatar image Ozirizz · May 03, 2017 at 08:53 PM 1
Share

Thanks for the proper solution. To add to add to this:

For people who are just starting out with this type of math or want to understand what the code does, here is an excelent post on what Bezier actually does, complete with pictures and animations.

avatar image Eldoir Ozirizz · May 24, 2018 at 01:44 PM 0
Share

Very useful link, thanks !

avatar image
3

Answer by dentedpixel · Jul 21, 2013 at 12:27 PM

You can also use a tweening class like LeanTween to accomplish the same effect:

 LeanTween.move( gameObject, [pt1, control2, control3, pt4], 1.0 );

You can also chain different paths together very easily, like

 LeanTween.move( gameObject, [pt1, control2, control3, pt4, pt4, control5, control6, pt7..... etc], 1.0 );

And if you do not wish it to move at a constant speed you can pass an easing function like:

 LeanTween.move( gameObject, [pt1, control2, control3, pt4], 1.0, {"ease":LeanTweenType.easeInOutQuad} );

Just a hint for anyone out there who doesn't want to have to write their own bezier interpreting code!

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

13 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

Related Questions

Determine transform.position of location between two waypoints on bezier path 0 Answers

How to dynamically set transform positions (for a Bezier curve)? 2 Answers

What is the reasoning of multiplying by Time.deltaTime 1 Answer

Accessing children of instances vs children of original prefab 1 Answer

does not work check if dead 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