How to stick to a surface of a cube and not sliding off while cube is rotating

Hello there,

I want to make a game that is similar to SpaceEngineers with walking on floors and so on.

I have coded the gravity boots script and it can easily walk / rotate around a cube (spaceship) that is in the scene, AS LONG as the cube not rotates.

i have got it to work when i use only y rotation axis.
on the x axis it reverses the deltarotation (i have deltarotation setup as a lastrotation of the cube - currentrotation of cube), but in this case it should not do that. also on x it sets y and z to 180

and not to speak about trying to do the below with 2 axis or more

this is how i rotate the cube:

//shipgrid script
//fixedupdate()
GetComponent<Rigidbody>().rotation *= Quaternion.AngleAxis(1f, transform.right);

and this is how i calculate deltarotation:

//shipgrid script
//update()
deltaRotation = transform.eulerAngles - lastRotationeulerAngles;

and this is how i apply it to the player:

//player movement script
//fixedupdate()
parent.transform.RotateAround(floorObject.position, floorObject.transform.right, ((ShipGrid) floorObject.transform.GetComponent<MonoBehaviour>()).deltaRotation.x);

idk if im missing something or if im completly off the road here -_-
please help,
thanks in advance

p.s. please dont mind my bad english my language is german ^^

well i did it myself YaY

Don’t ever step into euler angles when you’re dealing with true 3d rotations. You should do this with quaternions. Multiplying 2 quaternions will add their angles, dividing two quaternions will subtract their angles. Unity did not implement direct division. However for unit quaternions dividing one quaternion by another is the same as multiplying the first by the conjugate / inverse of the second.

r = a/b
r = a*bi / b*bi
r = a*bi / 1

It works the same way how you divide two complex numbers. You just have to rationalize the denominator ( Nenner) by multiplying the whole fraction by the conjugate. Since unit quaterions have a length of 1 the denominator would be 1 and can be ignored. So it’s simply a * Quaternion.Inverse(b). Though it always depends on your exact setup and if we talk about global or local space.

It’s great you solve your issues. However it would be great if you could actually share what your problem was or what you have done to solve it. Otherwise your thread is pretty useless ^^.

2 Likes

Yea, Quaternions are great for rotation.

but i used that to rotate the player around the object. I dont know if that cannot be done in a one liner but it works quite good :wink:

//Outside of function
private Quaternion lastRotation = Quaternion.identity;


//Function for rotation
Vector3 rotationEuler = -(Quaternion.Inverse(floorObject.transform.rotation) * lastRotation).eulerAngles;

transform.RotateAround(spaceship.position, spaceship.transform.up, rotationEuler.y);
transform.RotateAround(spaceship.position, spaceship.transform.right, rotationEuler.x);
transform.RotateAround(spaceship.position, spaceship.transform.forward, rotationEuler.z);

lastRotation = floorObject.rotation;

P.S
By the way the variable name was just eulerAngle but it actually was a Quaternion, so yea that helped ^^