- Home /
collision.rigidbody.AddExplosionForce();
Anyone knows why it doesnt work?
public float explosionStrenght = 100;
public float radius;
void Start() {
radius = this.gameObject.GetComponent<SphereCollider>.radius;
}
void OnCollisionEnter(Collision collision)
{
collision.rigidbody.AddExplosionForce(explosionStrenght, this.transform.position, radius);
}
Wasnt the ball supposed to "bounce-off" objects?
gives me the error: NullReferenceException UnityEngine.Rigidbody.AddExplosionForce (Single explosionForce, Vector3 explosionPosition, Single explosionRadius) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/NewDynamics.cs:730) Ball.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Scripts/Ball.cs:68)
Where do i gave it a null reference?
Maybe it has something with my rigidbody constrains being locked on the y pos? (?)
Are you sure that the "collision" object actually has a rigidBody attached to it?
Answer by robertbu · Sep 30, 2013 at 03:52 PM
I'm assuming you have a ball here bouncing off some other object. I'm assuming the ball has a rigidbody and the other object does not. The problem is that collision.rigidbody represents the rigidbody of the object that the ball hit, not the rigidbody of the ball. I believe you want:
void OnCollisionEnter(Collision collision)
{
rigidbody.AddExplosionForce(explosionStrenght, this.transform.position, radius);
}
Your answer
Follow this Question
Related Questions
How to activate a function on script B but its called from A 1 Answer
How to fix this error The current HDRP asset does not support volumetric clouds 0 Answers
KeyNotFoundException: The given key was not present in the dictionary 0 Answers
Creating a bounce effect? 3 Answers
Mario Mushroom Bouncy 1 Answer