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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

few question about quaternion slerp

Discussion in 'Scripting' started by Sylon87, Nov 27, 2018.

  1. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    hi everyone.

    i have a coroutine that looks like this

    Code (CSharp):
    1.     private IEnumerator StartRotation(
    2.         Quaternion curPos,
    3.         Quaternion targetPos,
    4.         float t
    5.     )
    6.     {
    7.         float counter = 0;
    8.         while ( counter < t)
    9.         {
    10.             counter += Time.deltaTime;
    11.             transform.rotation = Quaternion.Slerp(curPos,targetPos,counter);
    12.             yield return null;
    13.         }
    14.     }
    well, it's working, but i need two more things and i can't figure out hot to do it...

    - i want to be able to pingpong gameobject between this two rotation
    - maybe is a noob question but if i want that slerp complete the rotation in 1 second, ho i should set it?

    i'm confused.. someone can help me please?

    thank's you in advance
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Quaternion.RotateTowards might be more suitable for what you're trying to do.

    You can also use Quaternion.Angle to get the angle between quaternions, and you can use that to figure out how many angles to update each time you call Quaternion.RotateTowards so that you reach the target in a specific amount of time.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    if you want to complete a rotation is 1 sec:
    (this takes the shortest path so you can't use it to do a 360)
    Code (CSharp):
    1. Transform obj;
    2. Quaternion startRot;
    3. Quaternion endRot;
    4. float step = 0.0f;
    5. while(step < 1.0f){
    6. obj.rotation = Quaternion.Slerp(startRot, endRot, step);
    7. step += Time.deltaTime;
    8. yield return null;
    9. }
     
    Sylon87 likes this.
  4. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    in case i want a 360 degrees rotation?
     
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    In that case, you're better off rotating around an axis rather than trying to go from quaternion to quaternion.
     
    SparrowGS likes this.
  6. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196

    The point is i want a some kind of fake swinging movement, and slerp should be goot on this.

    But i actually have teo problem,

    - i want to start it like an impulse then slow down over time
    - and when i’m trying to go from -90 degrees to 135 it will cut and pass throught -180 degrees and not 0


    I’l check rotatetowards