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
1
Question by HNKMaster · Dec 04, 2014 at 06:43 PM · movementvector3curvebezier

Bezier movement is erratic

Hi, I was studying about bezier curves and how adapt into scripts, so I wanted to make a curve-based movement script with target:

 using UnityEngine;
 using System.Collections;
 
 public class CurveMov : MonoBehaviour {
 
     public bool on;
 
     public float way;
     public float duration;
     public float progress;
 
     public Vector3 mPoint; //Middle point of the curve
 
     public Transform target;
 
     void Start (){
 
         SetMid();
 
     }
 
     Vector3 BezierMov (float t, Vector3 p0, Vector3 p1, Vector3 p2){
         
         float u = 1f-t;
         float uu = u*u;
         float tt = t*t;
 
         Vector3 p = uu * p0;
         p += 2f * u * t * p1;
         p += tt * p2;
         
         return p;
     }
 
     public void SetMid (){
 
         mPoint = (target.position + transform.position)/2;
         mPoint.y += 10f; //Add some height
     }
     
     void Update () {
 
         if (on){
             progress += Time.deltaTime;
             way = Mathf.Clamp(progress / duration, 0f, 1f);
             transform.position = BezierMov(way, transform.position, mPoint, target.position);
         }
     }
 }

When "on" is true, it moves, but works very erratic. At start goes too fast, but when reach middle point, it moves slowly. How can I fix it?

Thanks in advance.

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

1 Reply

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

Answer by Bunny83 · Dec 05, 2014 at 12:57 AM

You shouldn't use the current position as first point as this will deform your curve as you go. The 3 points should be constant. So when you start moving you should save the start position in a variable and use that as the first point.

This should "fix" most of your problems. However keep in mind that a bezier curve isn't equally distributed and you will have a different speed at different points on the curve depending on the constallation of your control points.

This slight speed variation is a bit tricky to fix as it depends on where the points actually are. However if the points are in a light arc (<90°) the variation should be very small.

edit

To compensate the curve variation you could simply calculate the "speed" of the curve at the current point to approximately adjust the amount you have to move to get a constant speed:

 float speed = 2.0f; // desired speed in world units
 
 void Update ()
 {
     if (on)
     {
         float dist = (BezierMov(way + 0.01f, start, mPoint, target.position) - transform.position).magnitude;
         float f = 0.01f / dist;
         
         way += speed * f * Time.deltaTime;
         way = Mathf.Clamp01(way);
         transform.position = BezierMov(way, start, mPoint, target.position);
     }
 }

This should move your object from "start" to "target.position" at a speed of 2 world units per second.

If you want to move to the target in a specified time you would need to know the overall distance to calculate an appropriate speed. For this you would have to iterate through the whole path once when you start and store the approximate path length in a variable. That length divided by your desired duration would be the linear speed required to reach the target in the given time.

Comment
Add comment · Show 1 · 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 HNKMaster · Dec 05, 2014 at 01:24 AM 0
Share

Welp, that explain the erratic behavior. This solved the whole thing. Thanks a lot!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Controlling roll rotation when travelling along bezier curves 1 Answer

Find Vector3 point exactly between two gameobjects with an offset perpendicular to the line connecting the two gameobjects 1 Answer

iTween for Dynamically Moving Objects around a Sphere 0 Answers

Why isn't my teleport on collision script working? 0 Answers

Calculate vector exactly between 2 vectors 4 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