Top Down Shooter Help. Lookat wont look at negative position

so I have been trying to find this bug the whole day. I made a script that is supposed to look at the mouse position and then instantiate an arrow at an empty object which is rotated towards the cursor. However, it does work if the cursor is infront of the player but if it goes behind, it shoots and is rotated forward in the opposite direction the mouse is at. I tried many things like changing the y axis but they all mess it up. here is the script

#pragma strict
var mcursor : GameObject;
var mousePos;
var arrow_prefab : GameObject;
var angleBetween = 0.0;
var target : Transform;
var angle : float;
var emptytarget : GameObject;
var instan : GameObject;
var x : float;
function Start () {


function Update () {

mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);

mcursor.transform.position = mousePos;
mcursor.transform.position.z = 0.0;


emptytarget.transform.rotation.y = 0.0;
emptytarget.transform.rotation.x = 0.0;
if(mcursor.transform.position.x < transform.position.x){

emptytarget.transform.rotation = emptytarget.transform.rotation;

instan = Instantiate (arrow_prefab, transform.position, emptytarget.transform.rotation);


You could try something like this

Vector2 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float xDiff = mouseWorldPoint.x - transform.position.x;
float yDiff = mouseWorldPoint.y - transform.position.y;
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(yDiff, xDiff) * (180 / Mathf.PI));

I think I did it that way. I don’t know if it’s the best way, but it’s working.

When using

mousePos = Camera.main.ScreenToWorldPoint(mousePos);

you are not specifying a z depth. Think of it this way - a camera can only tell you your mouse position with respect to a 2D plane. In order for this data to be useful, you need to tell the camera how far away that plane should be.

To help visualize this, check out the frustum of a camera - see how it gets wider and wider the further out it is? That is why you need to specify a z depth.

Try something like this:

mousePos = Camera.main.ScreenToWorldPoint(mousePos + Vector3.forward * zDepth);