Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Use velocity variation or MoveTransition with predefined upper/lower bounds

Discussion in 'Scripting' started by Tetsubo, Jan 16, 2017.

  1. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Hello Unity Community,

    for a 2d game I want to move a kinematic rigidbody as in the picture shown. It's similar to movement on sin/cos curve. What I have is max velocity, upper and lower border, also "acceleration" can be defined. How to code the movement, pls help?



    As you see, the movement is similar to a sine/cosine curve. Because calculating sin/cos is a relatively costy operation, if there is a possibility to not use Mathf.sin() but something else that is faster and produces similar result/movement, I would preffer it, since I will have a lot of objects(10-20) in my mobile 2d game, that will move that way, so performance is an important factor..

    Thanks for any response!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Use an AnimationCurve to plot the path directly in the editor, use Evaluate to find where on the curve it should be and use MovePosition to set the position of the rigidbody to match.
     
  3. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Boah, I spent about 6 hours reading forums like insane about Lerping, Sin/Cos usage, SmoothDamp, but noone ever mentioned AnimationCurves at all! How exactly to use them? Do you know a good manual on that topic @GroZZleR ?

    Thanks a lot for the help so far! Gives me a whole new perspective... How performant is the solution with AinmationCurve, instead using Mathf.Sin() to calculate new position? //feel quite stupid right now, didn't even know that AimationCurve+Evaluate existed..
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Neither Sin/Cos or AnimationCurve's are prohibitively expensive, square roots are the one to watch out for.

    I whipped up a quick example:


    Code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AnimationCurveFollower : MonoBehaviour
    7. {
    8.    public AnimationCurve animationCurve;
    9.    public float duration = 2.0f;
    10.  
    11.    private Rigidbody rigidbody;
    12.    private float time;
    13.  
    14.    private float startingY;
    15.  
    16.    private void Start()
    17.    {
    18.      rigidbody = GetComponent<Rigidbody>();
    19.      time = 0f;
    20.  
    21.      startingY = transform.position.y;
    22.    }
    23.    
    24.    private void FixedUpdate()
    25.    {
    26.      time += (1.0f / duration) * Time.fixedDeltaTime;
    27.  
    28.      // continually move sideways
    29.      float x = transform.position.x + 1.0f * Time.fixedDeltaTime;
    30.  
    31.      // evaluate where we are on the curve to match the Y
    32.      float y = animationCurve.Evaluate(time);
    33.  
    34.      rigidbody.MovePosition(new Vector3(x, startingY + y, 0f));
    35.    }
    36. }
    37.  
    Just edit the AnimationCurve in the inspector. The top one is smooth and the bottom one is a lot of random noise just to illustrate.
     
    Tetsubo likes this.
  5. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    okay, thank you @GroZZleR ! Can I dynamically adjust the curve(from a script) - like setting upper and lower bounds of the curve?

    Btw. square root does not give me a lot options to make a sine similar curve... or am I wrong?