Hi, Im trying to get the force of an explosion to effect objects less the further they are from the center of the explosion. For some reason the method that im using dosent seem to have any impact of how far the objects seem to move in a way that I would expect, hopefuly im just missing something obvious and its a simple fix. Everything that is involved with the process is under “Void Knockback()”. Any help or suggestions would be appeciated, thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosivehealthmanager : MonoBehaviour
{
private Rigidbody2D rb;
public float currenthealth, maxhealth, effectscale;
public float radius, force;
public GameObject deatheffect;
void Start()
{
rb = GetComponent<Rigidbody2D>();
currenthealth = maxhealth;
}
void FixedUpdate()
{
if (currenthealth <= 0)
{
Knockback();
if (deatheffect != null)
{
var hitVFX = Instantiate(deatheffect, rb.transform.position, rb.transform.rotation) as GameObject;
hitVFX.transform.localScale = new Vector3(effectscale, effectscale, effectscale);
Destroy(gameObject);
}
else
Destroy(gameObject);
}
}
void Knockback()
{
Collider2D[] objects = Physics2D.OverlapCircleAll(transform.position, radius);
foreach (Collider2D obj in objects)
{
float distance = (Vector2.Distance(obj.transform.position, transform.position));
Vector2 direction = obj.transform.position - transform.position;
float forcemulti = force * 1/distance;
Debug.Log(obj.name);
Debug.Log(forcemulti);
if (forcemulti < Mathf.Infinity)
{
obj.GetComponent<Rigidbody2D>().AddForce((direction * forcemulti));
}
}
}
}