Question by
Soxbear · Aug 25, 2021 at 06:37 AM ·
rigidbodynot working
Forces not being applied to object
Hello I have an explosion script that is supposed to add forces to objects but doesn't. All of my debug.logs and console readouts indicate that a force should be applied, but none is.
This is the method.
Instantiate(Explosion, transform.position, transform.rotation); //Summon the particle
Enemies = Physics2D.OverlapCircleAll(transform.position, ExplosionSize/*, EnemyFilter, Enemies*/);
Debug.Log(Enemies.Length);
foreach (Collider2D Enemy in Enemies)
{
if (Enemy.tag == "Enemy")
{
Debug.Log("Enemy Found");
Vector2 Direction = Enemy.transform.position - transform.position;
float Distance = Direction.magnitude;
Mathf.Clamp(Distance, 1, ExplosionSize);
if (Direction.magnitude < 1)
Direction *= 1f / Direction.magnitude;
else
Direction = Vector2.ClampMagnitude(Direction, 1);
Enemy.transform.GetComponent<Rigidbody2D>().AddForce(Direction * ExplosionForce / Distance, ForceMode2D.Impulse);
Debug.Log(ExplosionForce / Distance);
}
}
HasCollided = true;
Any help is appreciated, as this is for a game jam.
Comment
Your answer
Follow this Question
Related Questions
RigidBogy.AddForce does nothing. 0 Answers
how disable/enable rigidbody and box collider 1 Answer
Objects not acting according to physics 1 Answer
Rigidbody.addForce is not working 10 Answers