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

How to rotate an object over Y axis while being aligned to the surface?

This code aligns the object to the surface:

``````Vector3 targetPoint = Vector3.up;
Quaternion targetRotation = Quaternion.FromToRotation(transform.up, targetPoint);
targetRotation.y = transform.rotation.y;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * alignSpeed);
``````

This code rotates the object:

``````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.

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.

1. 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:

``````        Vector3 normal = Vector3.up; //the normal of the surface, using 'up' for demo purposes

Quaternion deltaRot = Quaternion.FromToRotation(this.transform.up, normal);
Quaternion targRot = deltaRot * this.transform.rotation;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, targRot, Time.deltaTime * alignSpeed);
``````

Iâm willing to bet this may fix the âstickingâ youâre speaking of.

1 Like

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

Weird stuff like what?

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

Please give example of what âweirdâ is.

The cube bouces like mad over the surface when I apply force over a direction

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.

Dude, Thank you so much. This works perfectly. <3