Rotate towards mouse

Hey guys,

I have a tank sort of player, with the top section of the tank set to rotate towards the direction of the mouse, while the base stays the same.

Everything worked fine up until today, without any changes to the script or anything related to moving the tank.

What should happen is the top part should rotate a full 360 degrees to point towards the mouse, but now it only rotates a small ammount, and only if the mouse is on one side of the screen (its a top down game)

Here is the script

// LookAtMouse will cause an object to rotate toward the cursor, along the y axis.
//
// To use, drop on an object that should always look toward the mouse cursor.
// Change the speed value to alter how quickly the object rotates toward the mouse.
 
// speed is the rate at which the object will rotate
var speed = 4.0;
 
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);
 
        // Smoothly rotate towards the target point.
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    }
}

I recorded a quick vid showing the degree in which it rotates

you cant see the mouse but I’m rotating it completely around the tank player but it only rotates to a small degree.

Any ideas?

I would do this way:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
			
if (Physics.Raycast (ray, out hit, 500))
{
    this.gameObject.transform.LookAt(new Vector3(hit.point.x,this.gameobject.transform.position.y,hit.point.z));
}

Not really what I was looking for.

There is something wrong with the existing script, and proandrius, that snippet didnt seem to help anything.

I think it may be something set up weird in the scene, maybe something colliding when it shouldnt be

however, the posted link works for me.
from a quick look i cannot see what is wrong in your script. but i suggest to print the position of your targetpoint on screen to validate it.
you create a plane each frame and center it at players position. this is uneccessary as a plane is infinte. so simply create it once at start and center it in 0,0,0. i could imagine that your problem comes from getting target position coordinates relative to your player and not in world coordinates as required. but this is just a guess from the behavior of your turret. simply debug it to get every step correct.

aha!I fixed it. The key is here:

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

I accidentally had 2 cameras tagged as main camera, so it was screwing it up.

I feel completely silly now :stuck_out_tongue:

Thanks for all the help guys