- Home /
Shoot logic, bullet instantiation
Hey !
I'm having a issue when I want to instantiate a bullet going from my gun and to infinity. The bullet gets the right rotation from the code, but not the right force added. Can anyone help me out ?
Debug.Log("Shoot!");
//GunBlazeAnimation.Play();
GameObject clone;
Vector2 mousePos = Input.mousePosition;
GunBlazeAnimator.SetTrigger ("GunBlazeAnimation");
shotgunSource.Play ();
clone = (GameObject)Instantiate (bulletPrefab, GunBlazeGameObject.transform.position, GunBlazeGameObject.transform.rotation);
clone.transform.rigidbody2D.AddForce(mousePos * 10.0f);
Answer by robertbu · Apr 30, 2014 at 03:00 PM
First 'Input.mousePosition' is in screen coordinates. For use in the world space, you need to translate the screen coordinates into world coordinates. You can do that with Camera.ScreenToWorldPoint(). But you say:
The bullet gets the right rotation
If that is so, you don't need the mouse position at all. You can just do:
clone.transform.rigidbody2D.AddForce(clone.transform.right);
Note that 'clone.transform.right' might need to be 'clone.transform.up' or some other axis. It depends on what side of your bullet you consider forward when the rotation is (0,0,0).