Facing towards the mouse; tried many methods...

I’ve scoured the internet, found a hundred methods for this, attempted them all, and none had the desired effects.

I have my player moving forward using the following:

if (Input.GetKey("w"))
     {
     transform.position+=transform.TransformDirection(Vector3.forward)*mySpeed*Time.deltaTime;	
     }

Now I’m trying to make my character rotate towards the mouse - which should make it so the W key moves the player towards the mouse.

Based on several things I’ve seen around the internet, this is what I tried:

var mousePos : Vector3  = Input.mousePosition;
var worldPos : Vector3 = myCam.ScreenToWorldPoint(mousePos);
transform.LookAt(worldPos);

(myCam is set to the only camera in the scene, btw)

However, my character always moves UP (towards the camera, which is in top view mode). It doesn’t seem to be reading the mouse’s position at all, no matter where I have the mouse placed it moves up towards the camera.

Any help on this would be greatly appreciated! Since this is basically a 2d game, i can clamp my character to the ground if that helps???

Does the character turn to look towards the mouse?

Have you tried:

transform.position+=transform.forward*mySpeed*Time.deltaTime;

I use those code to make character move toward the mouse

These methods did not seem to work. Still in need of help.

The game is 2d, top down. The mouse will only be used to change the angle of the player, so I’m looking simply to rotate my character to the mouse’s position. I can’t imagine I’d need raycasting - the character will be moving around the ground plane, and just needs to rotate to the mouse position. I know I will have to use ScreenToWorldPoint, but I can’t seem to get it to work.

Thanks for the help. Any further suggestions?

I Think it shotld work:

LookAtMouse.js

function Update () {
	// Generate a plane that intersects the transform's position with an upwards normal.
    var playerPlane = new Plane(Vector3.up, transform.position);
    
    // Generate a ray from the cursor position
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    
    // Determine the point where the cursor ray intersects the plane.
    // This will be the point that the object must look towards to be looking at the mouse.
    // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
    //   then find the point along that ray that meets that distance.  This will be the point
    //   to look at.
    var hitdist = 0.0;
    // If the ray is parallel to the plane, Raycast will return false.
    if (playerPlane.Raycast (ray, hitdist)) {
        // Get the point along the ray that hits the calculated distance.
        var targetPoint = ray.GetPoint(hitdist);
        
        // Determine the target rotation.  This is the rotation if the transform looks at the target point.
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
        transform.rotation = targetRotation;
	transform.rotation.x = 0;
    }
}

Good script, change the line:

var playerPlane = new Plane(Vector3.forward, transform.position);

to

var playerPlane = new Plane(Vector3.up, transform.position);

for top down… :wink:

Thanks BigMister, I edited the script. I’m using this for a sidescrooler, thats why before was Vector3.forward ;D
I hope it helps.