- Home /
Objects moving weird after collision with other objects - rigidbody2d
i have a bear in my game, and i have an alien that explodes, the bear moves using Vector2.MoveTowards, and has rigidbody2d on it, when the alien explodes, it does (AddForce) effect on the objects around, however my bear starts moving weird and rotates weird too :( pls help
after explosion:
Because it is one of the objects around it?
Please post the relevant code.
it's just rigidbody, the bear moves around using vector2.$$anonymous$$oveTowards(), and has a rigidbody to detect collisions, the other monster explodes, and when he explodes he has that explosion effect to push objects away, and i used Rigidbody.addForce(), but because of the rigidbody attached to the bear, he moves using $$anonymous$$oveTowards but at the same time has force on it and that force stays a bit long and creates a weird movement for the bear, (he moves like an object in space) is there any way to make that effect not take that long, maybe something with forcemode or something pls help :(
No ones going to be able to debug your code for you without having access to the code.
this is the code that generates the explosion
void checkDistance()
{
if (player != null)
{
if (Vector2.Distance(transform.position, player.position) <= 1.75f)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, radius);
foreach(Collider2D collider in colliders)
{
Rigidbody2D rb = collider.GetComponent<Rigidbody2D>();
if(rb != null)
{
RigidbodyExplosion.AddExplosionForce(rb, explosionForce, transform.position, radius);
}
}
GameObject instance = SpawnObject.WithRotation(WormSplashEffect, transform);
Destroy(instance, 1f);
Destroy(gameObject);
}
}
}
and this the code for the explosion effect:
using UnityEngine;
public static class RigidbodyExplosion
{
public static void AddExplosionForce(this Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius)
{
var dir = (body.transform.position - explosionPosition);
float wearoff = 1 - (dir.magnitude / explosionRadius);
body.AddForce(dir.normalized * explosionForce * wearoff);
}
}
Can you move it into a comment ins$$anonymous$$d of an answer please. Or onto the original question ideally.