Rotate a capsule to make its top facing a point

Hello,

I got a capsule and I want it to rotate until its head ( in grey on the screenshot ) is facing the mouse position. ( it only rotate around the z-axis )
I guess it’s all about mathematics in this case but I’m kinda lost with Quaternions/Rotation, that’s why I’m asking for help.

alt text

You can do something like setting transform.up I’m pretty sure.
Put something like this in Update

// Gets vector from a to b.
Vector3 direction = (target.position - origin.position).normalized;
transform.up = direction;

Remember to declare these

//Set in editor
public Transform origin;
public Transform target;

To make it smooth, replace

Vector3 direction = (target.position - origin.position).normalized;

With

Vector3 direction = Vector3.Lerp(direction, (target.position - origin.position).normalized, 0.1f);
// The 0.1f is just an example, you can choose anything between 0 (does nothing) and 1 (not smooth)

Here is the standard rotation method:

    // Angular speed in radians per sec.
    float speed;

    void Update()
    {
        Vector3 targetDir = (0, 0, whateverPointYouAreRotatingTowards.transform.position.z) - capsule.transform.position;

        // The step size is equal to speed times frame time.
        float step = speed * Time.deltaTime;

        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
        Debug.DrawRay(transform.position, newDir, Color.red);

        // Move our position a step closer to the target.
        transform.rotation = Quaternion.LookRotation(newDir);
    }

I might not have that right if you are trying to rotate towards a point; I usually define targetDir like this:

Vector3 targetDir = target.transform.position - transform.position);

Anyway, that solution is a pretty standard rotation; there are also LERP and SLERP rotations, not honestly sure exactly the difference between them all; I know it has something to do with smoothness and acceleration of the rotation, but I try to get things working first and then worry about finessing after I have a commit uploaded.