Rotate object to mouse on the X axis (Help! )

Sorry if my English is bad :slight_smile:
I working on a platform-shooter game.
I have the player that walking with the right and left keys and I have the gun.
I that the gun will rotate toward the mouse according to the mouse position(X,Y).
I dont know how to do that but I have 2 Scripts:
1)rotate the gun on is X,Y,Z axis.
2)rotate the gun on is Y axis.
I dont know how to change them so they rotate only on the X axis,and I dont know
wich of the codes to choose. Maybe one of you can help me…Thanks :smile:

the codes:

// 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);
    }
}

and

private var mousePos : Vector3;
private var mouseX : int;
private var mouseY : int;
private var cameraDif : int;
var player : GameObject;
enum cameraDirection {x, y, z};
var camDirection : cameraDirection;

function Start () {

  if (camDirection == cameraDirection.x) {
		cameraDif = camera.transform.position.x - player.transform.position.x;
	}
	else if (camDirection == cameraDirection.y) {
		cameraDif = camera.transform.position.y - player.transform.position.y;
	} 
	else if (camDirection == cameraDirection.z) {
		cameraDif = camera.transform.position.z - player.transform.position.z;
	} 
	
}

function Update () {
	
    mouseX = Input.mousePosition.x;
    mouseY = Input.mousePosition.y;
	
	mousePos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));
	
	if (camDirection == cameraDirection.y) {
		mousePos.y = player.transform.position.y;
	}
	else if (camDirection == cameraDirection.x) {
		mousePos.x = player.transform.position.x;
	}
	else if (camDirection == cameraDirection.z) {
		mousePos.z = player.transform.position.z;
	}
	
	player.transform.LookAt(mousePos);
}

try this:

// 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.Normalize(Camera.main.transform.position - transform.position), 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);
    }
}

its rotate on the Y axis just like my code but I need it to rotate on the X axis

Are you sure? Did you try it? I modified the plane’s normal vector to make it face to the camera.

Yes I’m sure.Later I will upload the project files so you can see and test it.
Maybe someone else can help me?

Try this:

var speed = 4.0;

function Update () {
    // Generate a plane that intersects the transform's position with an upwards normal.
    var offset = Vector3.Normalize(Camera.main.transform.position - transform.position);
    var back = Vector3.Cross(Vector3.up, offset);
    var playerPlane = new Plane(Vector3.Cross(back, 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 think that AgentChaos is really working on a 2d shooter. That would be the only reason I would think that he would only want the X rotation.

The simple way is to take the first piece of code and replace one line of code:

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

This way, as your mouse pans accross the “side” view, the gun always points towards the point where the mouse is.

dont work.the rotate is on the Y axis :confused:

dont work :confused: its not doing anything,no on the X axis and no on the Y axis :confused:

Here is my project Files so you can downlod this and try your codes.
http://www.megaupload.com/?d=XU076DPW

the player is the sphere and he use the “move 2” script to moving.
the game object is what i want to rotate because later i will change him to my gun.
the scipts are: watchmouse - is my first code and the main camera use him but i disable him. watchmouse2 - is the original second code . and
watch mouse 3 - is the last code that hencz gave me.

Thanks guys for your help :slight_smile:

Help please ?

I opened your project and I couldn’t find my code so I replaced NewBehaviourScript with it and it actually WORKED.
The gameobject rotates around its X axis you only need to place the object in front of the camera and not under the camera!

Here is my objects positions:
Sphere-(0,1.8,0)
Camera-(0,1.8,-10)
GameObject-(0,2,0) (the Y was 0 and I changed this to 2)
But its rotate him on all of the axis (X,Y,Z)!
I use your code on the gameobject:

var speed = 4.0;

function Update () {
    // Generate a plane that intersects the transform's position with an upwards normal.
    var offset = Vector3.Normalize(Camera.main.transform.position - transform.position);
    var back = Vector3.Cross(Vector3.up, offset);
    var playerPlane = new Plane(Vector3.Cross(back, 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);
    }
}

How did you find out that it’s rotated on the XYZ?

Because when I in the game and not in the scene editor,I click on the empty gameobject
in the “hierarchy” menu and I see his rotation in the “Inspector” menu.

when you see numbers like 0.234324e -24 that means 0. it’s because of the inaccuracy of floats.
Attach a visible object to the gameobject and you’ll see that it rotates only on it’s X axis.

sometimes I see numbers like 0.234324e but normally I see regular numbers :stuck_out_tongue:
here I take a picture:
I attached a box to the gameobject and its rotate on the XYZ axis.
the box is where i draw red line :slight_smile:

http://img405.imageshack.us/img405/4143/111lbs.png

Try this:

var speed = 4.0;

function Update() {
    // Generate a plane that intersects the transform's position with an upwards normal.
    var playerPlane = new Plane(Vector3.back, 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);
    }
}

The same :frowning: on all the XYZ axis …
You think I need to quit or you can try help me with this problem?

try this :smile: worked for me:

var speed = 4.0;

function Update() {
    // Generate a plane that intersects the transform's position with an upwards normal.
    var playerPlane = new Plane(Vector3.back, 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);
        var side = Vector3.Cross(Vector3.up, (targetPoint - transform.position).normalized);
        var offset = Vector3.Cross(side, Vector3.up);
        //var up = Vector3.Cross(offset, (Camera.main.transform.position - transform.position).normalized);
        var up = Vector3.back;
        // Determine the target rotation.  This is the rotation if the transform looks at the target point.
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, up) * Quaternion.FromToRotation(Vector3.right, Vector3.up);

        // Smoothly rotate towards the target point.
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    }
}

Its work better now but its still rotate on all XYZ axis.
Its look good but not enough yet.the rotation angle is not perfect.
Here is the new one so please download it and help me…
Thanks alot for your help :slight_smile: !!!

http://www.megaupload.com/?d=X26JO1Y1

?