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
0
Question by alexandernst · Sep 02, 2016 at 01:50 PM · randompathbezier

Drawing random paths with quadratic bezier curves

I want to draw smooth* and random paths that my objects would follow and I decided to go with quadratic bezier curves (but I'm open to other ideas).

My code is moving my objects in a random, but not smooth* way.

Preview: https://youtu.be/Eg9PEKuH4zA

My question is: how can I make the direction changes smoother? Should I completely ditch my Bezier solution or is there a way I could polish my code to achieve what I want?

*smooth == no abrupt direction changes

My code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Lights : MonoBehaviour {
     public float paintHeight = -90.0f;
     public static int NUMBER_OF_LIGHTS = 3;
     private static int BEZIER_PATH_POINTS = 100;
     private float GOLDEN_COLOR_RATIO = 0.618033988749895f;
     private Light[] lights = new Light[NUMBER_OF_LIGHTS];
 
     Vector3 RandomPoint() {
         float obj_width = gameObject.GetComponent<RectTransform>().rect.width;
         float screenX = Random.Range(-obj_width / 2, obj_width / 2);
 
         float obj_height = gameObject.GetComponent<RectTransform>().rect.height;
         float screenY = Random.Range(-obj_height / 2, obj_height / 2);
 
         return new Vector3(screenX, screenY, -paintHeight);
     }
 
 
     Vector3 QuadraticBezierPoint(Vector3 startPoint, Vector3 endPoint, Vector3 vertexPoint, float t) {
         /*
          *         vertex
          *        /╲
          *       /   ╲
          *      /  p   ╲
          *     /  . .    ╲
          *    / .      ·   ╲
          *   /·           ·  ╲
          *  start           · end
          *
          *  0 < t < 1
          *
          * B(t) = (1 - t)^2 * P0 + 2 * (1-t) * t * P1 + t^2 * P2
          * 
          */
         return Mathf.Pow((1 - t), 2) * startPoint + 2 * (1 - t) * t * vertexPoint + Mathf.Pow(t, 2) * endPoint;
     }
 
     Color RandomColor() {
         float h = Random.Range(0.0f, 1.0f) + GOLDEN_COLOR_RATIO;
         h %= 1;
         return Color.HSVToRGB(h, 0.99f, 0.99f);
     }
 
     void Start() {
         for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
             GameObject light_obj = new GameObject();
             Light light = light_obj.AddComponent<Light>();
             light.type = LightType.Point;
             light.range = 10.0f;
             light.intensity = 3.5f;
             light.renderMode = LightRenderMode.ForcePixel;
             light.name = "Light" + i;
             light.transform.parent = gameObject.transform;
             lights[i] = light;
         }
 
         StartCoroutine("Move");
     }
 
     IEnumerator Move () {
         Dictionary<string, Vector3>[] light_points = new Dictionary<string, Vector3>[NUMBER_OF_LIGHTS];
         Dictionary<string, Color>[] light_colors = new Dictionary<string, Color>[NUMBER_OF_LIGHTS];
         for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
             light_points[i] = new Dictionary<string, Vector3>();
             light_colors[i] = new Dictionary<string, Color>();
             //light_points[i]["startPoint"] = RandomPoint();
             //light_points[i]["vertexPoint"] = RandomPoint();
             light_points[i]["endPoint"] = RandomPoint();
             light_colors[i]["nextColor"] = RandomColor();
         }
 
         while(true) {
             for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
                 light_points[i]["startPoint"] = light_points[i]["endPoint"];
                 light_points[i]["vertexPoint"] = RandomPoint();
                 light_points[i]["endPoint"] = RandomPoint();
 
                 light_colors[i]["currentColor"] = light_colors[i]["nextColor"];
                 light_colors[i]["nextColor"] = RandomColor();
             }
 
             for (int i = 0; i < BEZIER_PATH_POINTS; i++) {
                 float percent = (float)i / BEZIER_PATH_POINTS;
                 for (int j = 0; j < NUMBER_OF_LIGHTS; j++) {
                     lights[j].transform.localPosition = QuadraticBezierPoint(
                         light_points[j]["startPoint"],
                         light_points[j]["endPoint"],
                         light_points[j]["vertexPoint"],
                         percent
                     );
                     lights[j].color = Color.Lerp(light_colors[j]["currentColor"], light_colors[j]["nextColor"], percent);
                 }
                 yield return new WaitForSeconds(0.02f);
             }
         }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

[SOLVED] Problem with random generated path for 2D game 2 Answers

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

How to move an object on a random path with iTween 1 Answer

How to create a Mesh in a Closed Bezier Curve/Path 0 Answers

choose random waypoint 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