# rotation based on mouse position

OK, so what im working on is a basic dog fighter like game. I wan the object to face the crosshairs at all times (set to mouse position in another script) i thought the best way to do this was to set the rotation based on the mouse position as well but i cant seem to get the calculations for the rotation right because it calls the left side of the screen 0 not the center. Can someone set me on the right path?

here is my code idk if you’ll need it or if it will even help,

``````#pragma strict

//move variables
var leftThrust: ParticleSystem;
var rightThrust: ParticleSystem;
var centerThrust: ParticleSystem;
var speed: float=1;
var turn: float=1;
//rotate variables
var mouse_pos: Vector3;
var obeject_pos: Vector3;
var target: Transform;
var angle: float;
function Start () {

}

function Update ()
{
BasicMovement ();//begins on line 27
Turning ();// begins on line 88
}

function BasicMovement ()//basic movement Input
{
//zero out
if (Input.GetAxis("Horizontal")==0)
{
leftThrust.particleSystem.enableEmission=false;
rightThrust.particleSystem.enableEmission=false;
}
if (Input.GetAxis("Vertical")==0)
{
centerThrust.particleSystem.enableEmission=false;
}
if (Input.GetAxis("Horizontal")==0 && Input.GetAxis("Vertical")==0 )
{
rightThrust.particleSystem.enableEmission=false;
centerThrust.particleSystem.enableEmission=false;
}
//going left
if (Input.GetAxis("Horizontal")<0)
{
rightThrust.particleSystem.enableEmission=true;
transform.Rotate(0,0,-turn);
}
if (Input.GetAxis("Horizontal")<0 &&Input.GetAxis("Vertical")>0 )
{
rightThrust.particleSystem.enableEmission=true;
centerThrust.particleSystem.enableEmission=true;
}
//going Right
if (Input.GetAxis("Horizontal")>0)
{
leftThrust.particleSystem.enableEmission=true;
transform.Rotate(0,0,turn);
}
if (Input.GetAxis("Horizontal")<0 &&Input.GetAxis("Vertical")>0)
{
rightThrust.particleSystem.enableEmission=true;
centerThrust.particleSystem.enableEmission=true;
}
//going foward
if (Input.GetAxis("Vertical")>0)
{
centerThrust.particleSystem.enableEmission=true;
transform.Translate(-speed, 0, Time.deltaTime);
}
//going back
if (Input.GetAxis("Vertical")<0)
{
centerThrust.particleSystem.enableEmission=false;
transform.Translate(speed, 0, Time.deltaTime);
}
//going Up
if (Input.GetAxis("Jump")>0)
{
transform.Translate(0, Time.deltaTime,speed);
}
//going Down
if (Input.GetAxis("Jump")<0)
{
transform.Translate(0, Time.deltaTime,-speed);
}
}

function Turning ()
{
var angleX: float;
mouse_pos = Input.mousePosition;
mouse_pos.x = mouse_pos.x - obeject_pos.x;
mouse_pos.y = mouse_pos.y - obeject_pos.y;
angle = Mathf.Atan2( mouse_pos.y, mouse_pos.x)* Mathf.Rad2Deg;
angleX = angle + 90;
if ( angle < 90 && angle > -90)
{
transform.rotation=Quaternion.Euler(Vector2(-angleX,0));
}
}
``````

That’s some complicated trigonometry you’re doing there, and I’m not quite sure what it does.
The screen’s center you can get by using a `Vector2 screenCenter = new Vector2(Screen.width / 2, Screen.height / 2);`

Also, here’s some simpler code that could probably help you out; it’s what I’m using to control my spaceship.

``````public float rotationSpeed = 60.0f;

// Rotation around the Euler angles
float pitch = -Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
float yaw = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
float roll = -Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
Vector3 rotation = new Vector3(pitch, yaw, roll);
rotation = Vector3.Lerp (Vector3.zero, rotation, Time.deltaTime * rotationSpeed); // Smooth...
// PHYSICS - nah
//Vector3 rotation = new Vector3(roll, yaw, pitch); // rigidbody rotation wants the angles in a different order...?
``````var v3 = Camera.main.WorldToScreenPoint(object.pos);