Quaternions: Rotatingin to the direction of velocity

Good Day
I have a capulse shaped Bacteria with flagela which changes its velocity at random. Now I want it to always be rotated so its “tail” is in the oposite direction of movement. It tried it with the following code:

	rigidbody.velocity = new Vector3(0,1,0);
	Vector3 orignialDirection = rigidbody.velocity;
	rigidbody.velocity = CreateRandomVector() * _speedMultiplier;
	Vector3 nowDirection = rigidbody.velocity;

	Vector3 rotationAxis = Vector3.Cross(nowDirection,orignialDirection);
	float angle = Mathf.Sin((Vector3.Angle(orignialDirection, rigidbody.velocity)) * Mathf.Deg2Rad);
	
	Quaternion q = new Quaternion(rotationAxis.x, rotationAxis.y, rotationAxis.z, angle);
	_myTransform.rotation = q;

My only problem is it does not work correctly, sometimes it orients to the right direction but not always.

I start the bacteria moving upward (of the y axis) and its then facing the right direction.

So where is my error? What is wrong with my Quaternion? did I calculate the wrong angle?

Or do I initialise the Quaternion incorrectly= I also tried to initialise it this way:

Vector3 rotationAxis = (Vector3.Cross(nowDirection,orignialDirection)).normalized;
	float angle = Mathf.Sin((Vector3.Angle(orignialDirection, rigidbody.velocity)) * Mathf.Deg2Rad)/2;
	Quaternion q = new Quaternion(rotationAxis.x*Mathf.Sin(angle), rotationAxis.y*Mathf.Sin(angle), rotationAxis.z*Mathf.Sin(angle), Mathf.Cos(angle));`enter code here`

Any suggestions?

The simplest way to face the way you move is (Disclaimers: check to make sure you are moving first; give whatever up vector you like; snap or use RotateTowards):

Quaternion newFace = Quaternion.LookRotation(rigidbody.velocity);

One more problem with Degs and Rads is that Unity thinks 0 degrees is forwards, and goes clockwise. Sin/Cos thinks 0 rads faces right, and goes counter-clockwise. Mathf.Rad2Deg is simply (360/(2*PI)).

Anything needing real math, like you’re doing, it seems like you have to test every step and leave extensive notes. For example, in angle=Sin(...)*Deg2Rad I can’t figure out why the var is angle (sin doesn’t return an angle) or why not Rad2Deg (Sin returns radians, not degrees.)

Thank you, I Figured it out after a long time testing:
The easiest way to accomplish it is (without knowing much about quaternions)

    rigidbody.velocity = new Vector3(0,0,1);
	Vector3 orignialDirection = rigidbody.velocity;
	rigidbody.velocity = CreateRandomVector() * _speedMultiplier;
	Vector3 nowDirection = rigidbody.velocity;
	Vector3 rotationAxis = (Vector3.Cross(orignialDirection,nowDirection));
	float angle = (Vector3.Angle(orignialDirection, rigidbody.velocity)) * Mathf.Deg2Rad/2;
	Quaternion q = Quaternion.FromToRotation(orignialDirection, nowDirection);
	transform.rotation = q *transform.rotation ;

But If you want to calculate the things yourself you can use:

    float angle = (Vector3.Angle(orignialDirection, rigidbody.velocity)) * Mathf.Deg2Rad/2;
	Quaternion qbu = new Quaternion(rotationAxis.x*Mathf.Sin(angle), rotationAxis.y*Mathf.Sin(angle), rotationAxis.z*Mathf.Sin(angle), Mathf.Cos(angle));
	transform.rotation = qbu *transform.rotation ;

I hope that helps also someone else who has trouble with quaternions.