- Home /
Question by
BLGO94 · Sep 20, 2020 at 10:26 AM ·
unity 2drigidbody2d2d-physicscollision2d
Rigidbody ricochet effect error on 2D
Hello. I'm trying to make an bullet ricochet when hit an wall.
The bullet behaviour is described below:
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Collider2D))]
public class Bullet : PoolObj
{
[SerializeField]
private float initialForce = 0f;
Rigidbody2D rigidBody;
Collider2D collider2d;
void Awake()
{
rigidBody = GetComponent<Rigidbody2D>();
collider2d = GetComponent<Collider2D>();
}
public void SetInitialConfig(Vector3 position, float rotation)
{
rigidBody.angularVelocity = 0f;
rigidBody.velocity = Vector2.zero;
rigidBody.isKinematic = false;
gameObject.transform.position = position;
gameObject.transform.Rotate(0f, 0f, rotation);
rigidBody.velocity = transform.up * initialForce;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag.Equals("Wall"))
ReflectProjectile(collision.contacts[0].normal);
}
private void ReflectProjectile(Vector3 reflectVector)
{
rigidBody.velocity = Vector3.Reflect(rigidBody.velocity, reflectVector);
}
}
this code result is, when the object hit the wall, it start spinning in the same place while falling.
Can someone help me?
Thanks
Comment
Your answer
Follow this Question
Related Questions
How to move rigidbody relative to a moving object? 0 Answers
How can I allow the rope to correctly interact with Box and Polygon colliders? 0 Answers
2D Isometric Rigid Body Collision Resolution (collide and side) Not working 0 Answers
How to check if Rigidbody2d is moving 1 Answer
Disable click action when collision 1 Answer