Search Unity

Need help understanding Quaternion.

Discussion in 'Scripting' started by Theonesuperx, Aug 15, 2018.

  1. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24
    I watch the offical unity video on it and I am still a bit confused on it.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class gravity : MonoBehaviour {
    6.     public Transform target;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.        
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         Vector3 pos = (target.position + new Vector3(0,1.5f, 0)) - transform.position;
    16.        
    17.         Quaternion rotation = Quaternion.LookRotation(pos);
    18.  
    19.         Quaternion current = transform.localRotation;
    20.  
    21.         transform.localRotation = Quaternion.Slerp(current,rotation ,Time.deltaTime);
    22.         transform.Translate(0, 0, 3 * Time.deltaTime);
    23.     }
    24. }
    25.  
    I need help making things a bit more clear.
    If I have it right...
    Vector3 pos = (target.position + new Vector3(0,1.5f, 0)) - transform.position;


    This line here gets the position of the target compares it with its own transform.

    Quaternion rotation = Quaternion.LookRotation(pos);


    This line declares the rotation Quaternion variable which is equal to Quaternion.LookRotation(pos) The manual states that it creates a rotation with the specified forward and upwards directions

    transform.localRotation = Quaternion.Slerp(current,rotation ,Time.deltaTime);



      transform.Translate(0, 0, 3 * Time.deltaTime);


    Now I am a bit confused on these. On the first one, I am not sure how this code make the object rotates around the target. If I swap current and rotation around, the object will only follow the target. The last line of code, I am sure this code is needed to make the object move.

    But like I said, I am confused on how this code makes the transform rotate around the target. I want to learn this so I can make object perhaps rotate while rotating around a playing or it just seems very useful to learn. I am not sure how I can do other things with Quaternions right now.
     
  2. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24
    Help, please?
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    I skip Slerp, providing you understand how it works.

    I suggest you simple excercise.
    Get Game Object, stretched box for example, to see which direction is facing, and attach script with following examples.

    Don't use local rotation here, as it could confuse.
    a)
    Quaternion current = this.transform.rotation ;
    Debug.Log ( quaternion.euler ) ;

    b )
    Quaternion current = this.transform.rotation * Quaternion.euler (0, 30, 0) ;
    this.transform.rotation = current ;
    Debug.Log ( quaternion.euler ) ;

    c )
    this.transform.rotation *= Quaternion.euler (0, 30, 0) ;
    Debug.Log ( quaternion.euler ) ;

    d )
    Quaternion current = this.transform.rotation * Quaternion.euler (0, 30, 45) ;
    this.transform.rotation = current ;
    Debug.Log ( quaternion.euler ) ;

    e )
    Quaternion current = this.transform.rotation * Quaternion.euler (0, 30, 0) * Quaternion.euler (0, 0, 45) ;
    this.transform.rotation = current ;
    Debug.Log ( quaternion.euler ) ;

    f )
    Quaternion current = this.transform.rotation * Quaternion.euler (0, 0, 45) * Quaternion.euler (0, 0, 30) ;
    this.transform.rotation = current ;
    Debug.Log ( quaternion.euler ) ;

    Check other rotations and combinations as well

    g)
    // And if you want for example facing direction, you get
    Vector3 facingLocalDirection = this.transform.rotation * Vector.forward ;

    check also other vector directions.
    Use Debug.DrawLine, or Debug.DrawRay


    And don't use Update for moving objects,nor physics.

    Use FixedUpdate instead. Also you need changed deltaTime to fixedDeltaTime
     
    Last edited: Aug 17, 2018
    Theonesuperx likes this.
  4. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24

    Alirght, thank you. I will be sure to try this.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    So the advice for how to investigate how rotations work is solid. Just wanted to say:

    This is all wrong! Moving objects in Update is completely fine, as long as they're not simulated physics objects. Second, the value of deltaTime is the same as fixedDeltaTime in FixedUpdate, so you pretty much never need to use fixedDeltaTime, unless you've got some maths that require you to know how long a fixed frame is.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    deltaTime != fixedDeltaTime.
    FixedUpdate is not guaranteed to update same frequency frames, as Update.
    Also, Update is likely to execute multiple times, in period of FixedDelta time.
    Putting in Update, you ask CPU to do unnecessary calculations, which user will never see.
    Why I need move object faster than 30 FPS, or 60 FPS than needed.
    It is encouraging bad practice from the start.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    From the docs on deltaTime:
    If you're moving things without physics (moving .position instead of modifying a rigidbody's velocity), moving in FixedUpdate means that your character will move less often than the screen will render. That will look choppy, unless you're somewhat careful with also moving the camera in sync with physics, and setting all animations to update with physics. That will cause you to render the same frame twice in a row if there's no physics update between two updates.

    In essence, using FixedUpdate as a jerry-rigged way to have the CPU do less work is bonkers.
     
    Munchy2007 and Antypodish like this.
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Ah yes you are right on that. My wrong, since I use normally rigid body and physics, to move objects.
    Never the less, update should be used with cautious.

    Theonesuperx, if you not sure what we mean, I suggest try make an example of moving transform.position, in Update and FixedUpdate. You should see the differences.
     
  9. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    I've got 80 pages on it (vector math, then quaternions) at taxesforcatses-dot-com/vectorW/TOC.shtml

    For example, somewhere in chapter 2 or 3 it explains how that first line is logically a position minus a position, so is an _offset_ from you to a mount point on the target.
     
    Antypodish likes this.
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Nice source. But why your link is in cryptic form? Corrected.
    taxesforcatses.com/vectorW/TOC.shtml
     
  11. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Because my nettiquet is from the days when spammers posted links in comments sections to trick google. To avoid even the appearance of impropriety, you didn't write a live link to anything you have a connection to. Of course, live linking somewhere else is completely legit. The other part is, if someone can't take time to paste and demangle the link, they probably weren't going to read it anyway.
     
  12. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    I though you would be heading thoughts in this lines. Crawlers still can capture this link, withouth issue. They quite smart. Specially the bad spam crawlers. Unless you got some replay spam counter measures, I can remove link if request so.