- Home /
Unity2D problem with negative z rotation shooting bullet towards mouse click
I have what seems to me to be a very odd error, though I'm sure it's something simple I'm missing here. I'm rather new to Unity, and as such most of my code at the moment is adopted from tutorials.
I have the following code to shoot an object from my player's gun at where a user clicks with their mouse. The odd thing is that if I click right next to the gun, the bullet displays fine. But if I click even a small distance away the bullet rotates sideways in the z axis and is very difficult to see in the 2D view.
The bullet is fine if Z is 0 (which it is when clicking close to the player) but Z becomes progressively more negative negative the farther you click (10^-7 a short distance away and still only partially rotated, then 10^-6, then farther than that -- now almost invisible in the 2D view).
I am going to be reading up on Quaternions and the like as I do not understand how Unity uses them exactly yet but if anyone could point me in the right direction it would be much appreciated.
public GameObject GravityLiftShot;
private Vector3 mousePos;
private Vector3 objectPos;
private float angle;
private float shotSpeed = 1000;
void FixedUpdate()
{
//Point the gun at the mouse
mousePos = Input.mousePosition;
mousePos.z = 0;
objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90;
Vector3 rotationVector = new Vector3(0, 0, angle);
transform.rotation = Quaternion.Euler(rotationVector);
//Fire!
if (Input.GetMouseButtonDown(0))
{
GameObject shot = (GameObject)Instantiate(GravityLiftShot, transform.position, transform.rotation);
shot.transform.LookAt(mousePos);
shot.rigidbody2D.AddForce(shot.transform.forward * shotSpeed);
Answer by robertbu · Feb 09, 2014 at 06:38 AM
There are a number of potential issues here. Since I don't know how your game is setup, it is hard to figure out which ones are impacting your game. The top issue is line 23. LookAt() takes a position in 3D space. You are passing it a screen direction. You can solve it by:
shot.transform.rotation = Quaternion.LookRotation(mousePos);
Note this code is only going to work if 'GravityLiftShot' is a 3D object with the front of the object looking at positive 'z' when the rotation is (0,0,0). If 'GravityLiftShot' is a sprite you will need different code.
Another issue is the 'z' value of mousePos. Assuming all of your 2D action is happening at 'z' = 0, then you need to set it to '0'. Alternately you can set 'z' on line 11 to the distance in front of the camera. So if the camera is at z = -10, and the object is a z = 0, do on line 11:
mousePos.z = 10;
Sorry for the much delayed response (had a lot of things suddenly go on and wasn't able to work on this) but yes that was indeed my issue (I was trying to use the functions on a sprite).
I was able to find someone doing something similar with Javascript and use their code ins$$anonymous$$d which was a bit simpler (as expected since I'm messing with 2D only) many thanks for the help! I'd thumbs up but I'm not active enough yet =/