rotation question...

hey guys

i have an issue with rotations and quaternions, and i don’t even know what to search for.

i have this setup:

  • no gravity in unity
  • sphere trigger, giving constant negative force to the fireball
  • fireball in the trigger with the script below on it.
  • z-position reset of the fireball in late update to achieve 2d gameplay.

this is what is happening at the very top and the very bottom of the circle:

video

the y direction is changing suddenly. i tried it with “LookAt” and the now in use “LookRotation”.

does anyone know what i can do so the y direction doesn’t suddenly switch? i hacked in a hacky solution, but that also turns the model attached to the rigidbody around, and that looks very stupid :wink:

script:

function FixedUpdate() {
	if(initialSpeed != 0) {
		var relativePos = magneticAsteroid.transform.position - transform.position;
		var rot : Quaternion = Quaternion.LookRotation(relativePos); 
		transform.rotation = rot;
		
		//dirty hack so the orbiting is working...
		if(transform.rotation.y <= 0) {
			rigidbody.AddRelativeForce(0,-7,0);
		}
		
		if(transform.rotation.y >= 0) {
			
			rigidbody.AddRelativeForce(0,7,0);
		}
	}
}

function LateUpdate() {
	if(moveInProgress) {
		gameObject.transform.position.z = 0;
		
	}	

}

Hi, welcome to the forum!

Quaternion.LookRotation has an optional second parameter that specifies the upward direction of the object during the rotation. If you use the axis of rotation/orbiting for this parameter, you won’t get the sudden changes of orientation.

thank you so much! that did the trick. i read that in the documentation, but didn’t think that was the thing, causing the problem. guess i don’t understand quaternions at all :wink:

anyway, thanks for the welcome and i’m glad to be logged in here with you.