AddExplosionForce not affecting every rigidbody
public float radius = 5.0F, power = 10.0F;
void Update()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
if (Input.GetMouseButtonDown(0))
{
Shoot();
}
}
void Shoot()
{
Vector3 fwd = muzzle.TransformDirection(Vector3.forward);
RaycastHit hit;
if (Physics.Raycast(muzzle.position, fwd, out hit, Mathf.Infinity))
{
Vector3 explosionPos = hit.point;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider c in colliders)
{
Rigidbody r = c.GetComponent<Rigidbody>();
if(r != null)
r.AddExplosionForce(power, explosionPos, radius);
if (c.gameObject.tag == "Body" || c.gameObject.tag == "Head")
{
LimbDamage _ld = c.transform.gameObject.GetComponent<LimbDamage>();
if (!_ld._eh.dead)
{
_ld.KillByRailgun();
}
}
}
}
}
When ever i click the mouse button it affects a certain amount of objects for a short amount of time and then it stops completely. Everything is set up , the raycast works , the colliders are there , the tags are there , this should work...
make sure the rigidbodys are so heavy that they cant be moved and make sure they are in the radious, use a debug.log inside the foreach and print c name and make sure all the bodys that should be affected are in there.
Answer by t0ky00 · Jan 19, 2019 at 11:01 PM
Well i found a fix , this specific line caused the issue for some reason
if (c.gameObject.tag == "Body" || c.gameObject.tag == "Head")
{
LimbDamage _ld = c.transform.gameObject.GetComponent<LimbDamage>();
if (!_ld._eh.dead)
{
_ld.KillByRailgun();
}
}
Basically i just changed it to -
if (Physics.Raycast(muzzle.position, fwd, out hit, Mathf.Infinity))
{
Collider[] colliders = Physics.OverlapSphere(hit.point, radius);
foreach (Collider c in colliders)
{
Rigidbody r = c.GetComponent<Rigidbody>();
if (r != null)
{
print(r.gameObject.name);
r.AddExplosionForce(power, hit.point, radius, 3f);
LimbDamage _ld = c.transform.gameObject.GetComponent<LimbDamage>();
if (_ld != null)
{
_ld.KillByRailgun();
}
}
}
}
No idea why the one above didn't work...
Your answer
Follow this Question
Related Questions
Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 Answers
How do I get objects in front of the player to be blasted away? 1 Answer
Ray cast to object to transform, instead all objects with script transform. 0 Answers
How to ensure raycasts hit colliders inside of trigger colliders on objects that have a rigidbody? 0 Answers
how to prevent a kinematic rigidbody from going through static colliders 0 Answers