Using a raycast's direction in parent's Y-rotation

Hi! I have a controllable character in 3d and I’m casting a raycast from it’s center and for the direction of the raycast I’m using rigidbody.velocity.normalized. Now, with debug.drawRay I’m seeing that the raycast works properly, but I can’t figure out how to get the rotation of the ray to use for the player’s Y-rotation. The goal would be to detect the direction of the ray so that my character would have a target Y-rotation or something like that. Hope this question is simple enough. I didn’t want to resort to UnityAnswers but here I am anyway.

The answer will depend on some context you did not provide:

  • Is the character always aligned with Vector3.up?
  • Is the velocity always parallel to the XY plane (i.e. no ‘y’ component)?
  • Did you construct your object so that the front is facing positive ‘z’ when the rotation is (0,0,0).
  • Do you need immediate rotation, or a smoothed rotation.
  • If you need a smoothed rotation, do you want that smoothing eased or at a constant speed?

If your answers are yes, yes, yes, no, no, then you can do this:

 var v = rigidbody.velocity;
 if (v != Vector3.zero) {
     transform.rotation = Quaternion.LookRotation(v);
 }

Other answers to the five questions will result in different code.