Question by
chetan312 · Jul 02, 2020 at 12:35 PM ·
addexplosionforce
addExplosionforce is not working properl;y
hii, I am using AddExlosionForce for a bomb explosion but the effect of explosion is very little even i change explosion power from 30 to 30k...any idea, what i am doin wrong? thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestingExplosiveforce : MonoBehaviour
{
public float radius = 20.0F;
public float power = 30000.0F;
void Start()
{
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
Debug.Log("testing hit colliders" + hit.gameObject.name);
if (rb != null)
rb.AddExplosionForce(power, explosionPos, radius, 4.0F);
}
}
}
Comment
Where do you change the value? In the editor, not in the public float power = 30000.0F
right?
thanks for your response, actually u are right , i totally forgot to change in the Editor, but same problem occur in my original grenade Script and i dont use Public field there ....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bazookaBulletFunctionality : $$anonymous$$onoBehaviour
{
private Rigidbody rb;
private int radius=10;
private float power = 1000;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
rb.AddForce(transform.forward*2500f);
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
Explode();
}
private void Explode() {
Vector3 ExplosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(ExplosionPos, radius);
foreach (Collider hit in colliders)
{
Rigidbody rb = GetComponent<Rigidbody>();
Debug.Log("hit object here with name" +hit.gameObject.name);
rb.AddExplosionForce(power, ExplosionPos, radius, 3);
}
Destroy(gameObject);
}
}
okay, i think i found my mistake ...in Foreach loop I just used getComponent() ins$$anonymous$$d of hit.GetComponent()...Thanks