Rotating / Moving an Egg

Hi there
I try to use an Egg as a player Character. The Player should be able to rotate the egg in four directions (up/down/left/right). I tried different approaches to achieve my goal but do not seem to get nearer to the required output.

LocalEulerAngles:

{
    switch (_requiredDirection)
    {
        case MovementRequest.Up:
            _playerBody.transform.localEulerAngles= new Vector3(0, 0, 90);
            break;
    }
    _requiredDirection = MovementRequest.None;
}

This rotates the egg but it isn’t smooth. It simply puts the egg in that angle in one frame and thats it.

The same result when using rotate instead:
_playerBody.transform.Rotate(new Vector3(0, 0, 1), 90);

This is when “Automatic Center of Mass” is selected on the RigidBody. It rotates to the 90 Degree position in one Frame and stays there.

If I change the center of mass manualy, it also jumps to the 90 degree angle. but bounces a while afterwards.

So what I think I need to have is a force that “pushes” the egg causing a rotation and then stopping that force once the required position is achieved.

As this seems to be quite a basic task I am already happy for the right direction. Checked a lot of rotation documents and Videos but none seems to match my requirements

Thanks

If I need to privide any additional information I am happy to give them

	void FixedUpdate()
	{
		Vector3 force=Camera.main.transform.forward*Input.GetAxis("Vertical")+Camera.main.transform.right*Input.GetAxis("Horizontal");
		rb.AddForceAtPosition(force*10,transform.position+Vector3.up);
	}
1 Like

Thanks a lot. The movement looks nearly as I wanted it to look. But I do not want a continuous movement and limit the movement to the four axes. So no Diagonal and one single buttonpress (WASD) should only result in one small 90 Degree movement.

(I can code the Input-stuff itself, just need a single “Rotate 90 degrees in Direction X” example and can add the rest myself)

Base on up/ down/ left / right, set a variable that is desiredRotation to perhaps 0, 90, 180 or 270

Then in your Update() loop, always move another variable currentRotation towards that value.

Here’s more, including code examples:

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

1 Like