Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Slerping(?) for an exact time

Discussion in 'Scripting' started by Slarti-42, Feb 20, 2021.

  1. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    Hi folks,

    I have been playing around with this now for a while and I cannot make it work.

    I have multiple objects that I want to turn towards a direction, each taking a different time and ideally, halfway through, is the fastest turn rate. I guess I need there then an acceleration/deceleration constant?

    So I calculated degrees/seconds as a kind of speed measurement, but I couldn't make it work. :confused:

    Anyone could lend me a hand, please?

    Thanks,
    Slarti
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Post the code up, it’ll be easier for people to help.
     
  3. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    Not much to show ;):
                    rotation.Value = Quaternion.Slerp(rotation.Value,targetRotation, ???);

    I am looking for the question marks.
    as an example, I want in 10 seconds it slerps to 180 degrees to make it simple. With this information, you know how many degrees it turns in 1 second.

    The question is how do I express this in these ???.
     
  4. A_Savvidis

    A_Savvidis

    Joined:
    Jul 21, 2016
    Posts:
    97
    I always liked that site as it gives a list of examples. Let your mouse hover on a image to see it in action. Then click on the image you like and at the bottom you will find the math to implement for that transform.
     
    Slarti-42 and mopthrow like this.
  5. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Easy pattern for this kind of use case:

    Code (CSharp):
    1. // set up your easing curve in the editor
    2. // there's a bunch of defaults
    3. public AnimationCurve easeCurve;
    4.  
    5. float time;
    6. public float duration = 2; // or whatever
    7.  
    8. // then in your animation loop (here represented as a coroutine)
    9. IEnumerator AnimCo () {
    10.    // important note:
    11.    // When using slerp/lerp you are going from point A to point B
    12.    // Unlike movetowards in which you're going from <current pos> to point B
    13.    var startRotation = rotation.Value;
    14.  
    15.    while( time < duration ) {
    16.        time += Time.deltaTime;
    17.        rotation.Value = Quaternion.Slerp( startRotation, targetRotation, easeCurve.Evaluate( time / duration ) );
    18.        yield return null;
    19.    }
    20. }
    If you're in a situation where you need to dynamically modify the rotation target on the fly, this method won't work. You will need to use RotateTowards instead.
     
    Last edited: Feb 20, 2021
    Bunny83 and Slarti-42 like this.
  6. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    @Madgvox, thanks, buddy. As a matter of fact, I had this, but as you pointed out the start position had to be at the initial point. That's why mine wasn't working.
     
    Madgvox and Bunny83 like this.