need help with making player look at mouse

Im trying to make a top down shooter and i am stuck on getting the player to rotate to look at where the mouse is on the screen. i have searched google for a help but all the scripts i found make my player spin out of control in all directions.

if some one could show me the script i would really appreciate it… or if you can point me in the direction of a top down shooter tutorial that would be even better =]
…EDIT…

thanks for the replies,
right now i am using this script.

var rotateSpeed = 5.0;
var hitdist = 28.0;

function Update () 

{   
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var targetPoint = ray.GetPoint(hitdist);
    var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,
    rotateSpeed * Time.deltaTime);
}

now i am trying to work out how to make it only rotate alone one axis to stop the player aiming up and down as the mouse moves towards and away the player.

…EDIT2…

i found out how to do it.

var speed = 4.0;

function Update () {

    var playerPlane = new Plane(Vector3.up, transform.position);
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hitdist = 0.0;

    if (playerPlane.Raycast (ray, hitdist)) {

        var targetPoint = ray.GetPoint(hitdist);
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

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

once i work out the rest of my problems to get a simple topdown prototype i will upload my assets for others to use. it will be a useful starting point to speed up developing a game

Generally you will need to do three steps:

  1. Convert the mouse's screen position into a world-space ray
  2. cast the ray into your scene and find the ray's intersection point
  3. Use the ray's hit point as the target for LookAt

Eg:

var lookTarget : Vector3;

function Update() {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit)) {
        lookTarget = hit.point;
    }

    transform.LookAt(lookTarget);

}

If you want your object to gradually turn towards the target instead of snapping its rotation, you could modify the script like this:

var lookTarget : Vector3;
var turnSpeed : 20.0;

function Update() {

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit) {
        lookTarget = hit.point;
    }

    // rotate towards target
    var lookDelta = (hit.point-transform.position);
    var targetRot = Quaternion.LookRotation(lookDelta);
    var rotSpeed = turnSpeed * Time.deltaTime;
    transform.rotation = Quaternion.RotateTowards( transform.rotation, targetRot, rotSpeed );

}

(untested code - leave comments if you find typos!)

I'm not sure how your game really works but if you just want something to look at your mouse then:

1) Use this to get the mouse position: Input.mousePosition

2) Use THIS to get the 3D world position for the given mouse position:

     var MouseScreenPosVector3 : Vector3 = camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Screen.height - Input.mousePosition.y,camera.nearClipPlane));

3) Finally, use this to make the mouse look at that position:

         transform.LookAt(MouseScreenPosVector3);   // where 'transform' is the transform of your player 'MouseScreenPosVector3' is the Vector3 position of the mouse positions you attained in step 2

Look for the 'Evac City' tutorial (it covers this).

As for how to code it, there's no 'the script', per se. Typically, however, the solution will involve getting the mouse position and the player position into the same space (e.g. world space or screen space) and then using Atan2() or Quaternion.LookRotation() to (directly or indirectly) align the player towards the cursor.

If you have some code already but it's not working, consider editing your post to include that code (someone may be able to spot the problem for you).