Instantiating a bullet object from a 2D object that looks at mouse cursor

Hi, I have a 2D object that continuously looks at the mouse cursor. I am using an overheard view. Here is the code that I used for this:

Vector3 screen_pos = Camera.main.ScreenToWorldPoint (new Vector3 (mouse_pos.x, mouse_pos.y, 0));
transform.LookAt ( screen_pos, Vector3.forward);

Before I changed to using the mouse cursor I was just using left/right arrow keys to rotate the object and I was able to instantiate bullet objects using this code:

Instantiate (BulletPrefab, transform.localPosition+(transform.up *.5f)+(transform.right*-.3f) , transform.localRotation);  // bullet instantiates from front right

The above code worked fine before but now with the mouse cursor code added in, the bullets move in random directions and sometimes instantiate in the wrong position.

Thanks.

You are currently placing your bullet with hard-coded offset. Try this instead:

// get the vector from current pos to mouse (you have to cache your screen_pos)
Vector3 direction = (screen_pos - transform.position).normalized;

// Instantiate your bullet with 0.5 units offset to your position and your rotation
Instantiate(BulletPrefab, transform.position + direction * 0.5f, transform.rotation);

This code will crash at .normalized if your cursor’s position equals your 2D-object’s position because normalize will divide by the length of (screen_pos - transform.position) which, in that case, is zero.

To circumvent annoying bugs (bullet flies at camera…) with this system make sure to have the z-value of both screen_pos and transform.position aligned:

Vector3 screen_pos = Camera.main.ScreenToWorldPoint (new Vector3 (mouse_pos.x, mouse_pos.y, 0));
screen_pos.z = transform.position.z;

You’re using transform.position + transform.up.
transform.up is relative to the object, so when you rotate it with LookAt, the up direction changes as well. If you want it to be always on top of the object, regardless of where the object is facing, you can use Vector3.up instead.

If this is not what you want, you better explain yourself better, and a screenshot or schematic will help.

Ok, I am willing to start from scratch on this rotation idea so if that means using an alternative to lookAt() I’m fine with that. I attached a schematic and I will try to explain it in further detail. The game is 2D with an overhead/birds eye view of the spaceship. Basically wherever the mouse cursor moves on the screen, I want the spaceship to rotate only around the z-axis ( basically if a rod came out from the screen, it would rotate around that as shown in the schematic.) and have the front facing towards the mouse cursor.

So in the schematic pretend the arrow is the cursor and the front is always facing the cursor.
The green triangles in the schematic are the bullets. When the bullets are instantiated, the front of the bullet is in the same direction as the spaceship.

If I explained anything poorly or used wrong terminology please correct me because I am trying to learn the lingo along the way. Thanks.

Alright finally after all my searching I found the answer. The solution I found doesn’t use lookat() but does the job.

Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         diff.Normalize();
 
         float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);