Search Unity

[SOLVED] Rotate an object over Y axis while aligned to surface?

Discussion in 'Scripting' started by Rusoski, Sep 15, 2017.

  1. Rusoski

    Rusoski

    Joined:
    Nov 6, 2013
    Posts:
    63
    How to rotate an object over Y axis while being aligned to the surface?

    This code aligns the object to the surface:

    Code (CSharp):
    1. Vector3 targetPoint = Vector3.up;
    2. Quaternion targetRotation = Quaternion.FromToRotation(transform.up, targetPoint);
    3. targetRotation.y = transform.rotation.y;
    4. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * alignSpeed);
    This code rotates the object:

    Code (CSharp):
    1. transform.Rotate(Vector3.up * Time.deltaTime * speed * -50);
    But for some reason it does not work, it rotates,but ar some degrees it get stuck.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    1) Where'd you get the first snippet of code?

    I ask because you're manipulating the Quaternion components directly.

    Not the 'y' coordinate of a Quaternion doesn't necessarily directly correlate to the 'y' component of euler angles. A quaternion has 4 components, x,y,z,w. They represent a set of complex numbers that well I'm not going to get any further into as quat's are complicated as all hell to explain, especially since my understanding of them though good, isn't good enough to explain them to other people in a coherent manner.

    So... either a| you know how quaternion's work in which case not sure why you'd have this question, or b| you're not sure how they work, and modifying that component is strongly urged against being done.

    2) What do you mean it gets stuck at some degrees? Is there some pattern in what degrees it gets stuck at? Can you share an image/video of this with us?

    3)Here might be a better piece of code for you to align with a surface:
    Code (csharp):
    1.  
    2.         Vector3 normal = Vector3.up; //the normal of the surface, using 'up' for demo purposes
    3.  
    4.         Quaternion deltaRot = Quaternion.FromToRotation(this.transform.up, normal);
    5.         Quaternion targRot = deltaRot * this.transform.rotation;
    6.         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, targRot, Time.deltaTime * alignSpeed);
    7.  
    I'm willing to bet this may fix the "sticking" you're speaking of.
     
    Carterryan1990 likes this.
  3. Rusoski

    Rusoski

    Joined:
    Nov 6, 2013
    Posts:
    63
    Thank you so much for your responce, but your method just did not work, weird stuff happened.

    And no, I have no idea how Quaternions work
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Weird stuff like what?

    I used the same exact code, and it worked just fine.

    Please give example of what "weird" is.
     
  5. Rusoski

    Rusoski

    Joined:
    Nov 6, 2013
    Posts:
    63
    The cube bouces like mad over the surface when I apply force over a direction
     
  6. Rusoski

    Rusoski

    Joined:
    Nov 6, 2013
    Posts:
    63
    I have solved the problem by adjusting the drag and the angular drag, I had them set to 0, with this adjustments, it works perfect.
     
  7. Carterryan1990

    Carterryan1990

    Joined:
    Dec 29, 2016
    Posts:
    79
    Dude, Thank you so much. This works perfectly. <3