smooth raycast LookAt ?

I’ve looked through previous answers and posts in order to help me to construct a piece of javascript, I’m very new to coding so I’m pretty much making frankensteins out of other peoples scripts trying to get stuff to work and in this case it just isn’t working!

Would be grateful if someone could help me to get this code working properly! It’s supposed to rotate and object in all three axes to face the mouse position, and to do it smoothly instead of jumping immediately to the position.

var speed = 4.0;
var hit : RaycastHit;
function Update ()
    {
    var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
    if (Physics.Raycast (ray,hit)) {
    
        var targetPoint = ray.GetPoint;
        var targetRotation = Quaternion.LookRotation(
                               targetPoint - transform.position);

        transform.rotation = Quaternion.Slerp(
                     transform.rotation,
                     targetRotation, speed * Time.deltaTime);
    }
}

targetPoint = hit.point;

ray is the imaginary line in the game world, shooting straight out of the camera. It is input to the raycast line, giving you hit as the output. So, of course we check hit afterwards. Yes, that is very, very tricky. Just by looking at RayCast(ray, hit); there’s no way to know input from output.

Alternately (and this may have been the cause of the trouble,) hit also tells you how far the ray went before hitting, in hit.distance. So, you could compute the hit point using ray.GetPoint(hit.distance);. Using just ray.GetPoint is an error – you have to tell it how far along the line you are in parens (see the docs,) but UnityScript often “helpfully” guesses the answer is zero.