Question by
ChuckFishs · Dec 02, 2020 at 12:33 AM ·
2d gamebugrigidbody2daddforcegun
Bug With 2D Gun
So I have a gun script the follows the mouse and I add force to the right, but in my character controller script if my player moves left it change the scale from 1 to -1 basically changing where the player is looking. So when I move left the gun also faces left but when I shoot the bullet goes right any help? Here is the code:
public class ShootGun : MonoBehaviour {
public Transform Gun;
Vector2 direction;
public GameObject Bullet;
public float BulletSpeed;
public Transform ShootPoint;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = mousePos - (Vector2)Gun.position;
FaceMouse();
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}
void FaceMouse()
{
Gun.transform.right = direction;
}
void shoot()
{
GameObject BulletIns = Instantiate(Bullet, ShootPoint.position, ShootPoint.rotation);
BulletIns.GetComponent<Rigidbody2D>().AddForce(BulletIns.transform.right * BulletSpeed);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613020739im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Composite collider 2D mysteriously adds x velocity to player rigidbody 1 Answer
Trying to add force to an object... is this done correctly? 0 Answers
Predicting max jump height of a force impulse 1 Answer
[2D] Can't AddForce to x but y works fine. 1 Answer
How can I achieve constant Rigidbody2D.AddForce() behavior in the canvas? 0 Answers