- Home /
Question by
Thomasdb05 · Apr 06, 2020 at 10:51 AM ·
unity 2daddforceshootingshooter
Why doesn't AddForce work upwards?
I am making a 2D top down shooter so I made an enemy that when it gets hit, shoots projectiles in 4 directions and destroys itself. It works fine shooting upwards and downwards but for some reason doesn't to the left and right, it just spawns the 2 circles but they don't move, it does destroy itself though. Sorry for the messy script, this is the script:
public GameObject explosion;
public Transform enemycr;
public float explodeforce = 100f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collisioninfo)
{
if (collisioninfo.collider.tag == ("bullet"))
{
GameObject explode1 = Instantiate(explosion, enemycr.position, enemycr.rotation);
Rigidbody2D rb1 = explode1.GetComponent<Rigidbody2D>();
rb1.AddForce(enemycr.up * explodeforce);
GameObject explode2 = Instantiate(explosion, enemycr.position, enemycr.rotation);
Rigidbody2D rb2 = explode2.GetComponent<Rigidbody2D>();
rb2.AddForce(-enemycr.up * explodeforce);
GameObject explode3 = Instantiate(explosion, enemycr.position, enemycr.rotation);
Rigidbody2D rb3 = explode3.GetComponent<Rigidbody2D>();
rb3.AddForce(enemycr.right * explodeforce);
GameObject explode4 = Instantiate(explosion, enemycr.position, enemycr.rotation);
Rigidbody2D rb4 = explode4.GetComponent<Rigidbody2D>();
rb4.AddForce(-enemycr.right * explodeforce);
Destroy(gameObject);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Having problem with killing aliens in Space Invaders. 2 Answers
Problem with Wall Jump - 2D 0 Answers
Inconsistent add force across PCs 0 Answers
AddForce() in axis x and y not working 0 Answers