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