- Home /
Forces arent being applied as expected
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));
}
}
}
}
Answer by olejuer · Feb 02 at 08:54 AM
You are running into problems with the force being applied for a very short amount of time (namely a single frame).
You probably want to use ForceMode2D.Impulse mode as a second parameter to the AddForce call. https://docs.unity3d.com/ScriptReference/ForceMode2D.Impulse.html
I am actually not sure how it handles time exactly. But you will probably want to divide your force by Time.deltaTime to keep the result independent of the frame rate. But I am not sure, because I haven't dealt with forces in Unity for a while. Maybe that's what the Impulse mode does.
[edit] found this https://forum.unity.com/threads/difference-between-forcemode2d-force-and-forcemode2d-impulse.649858/
Thanks for the help, i've added the impules force mode and divided the force by Time.delta time and it runs much closer to how I wanted it to originally work. For some reason though the distance to the center of the explosion still dosen't seem to effect the distance it travels due to the increased force applied to the objects, with the (forcemulti*direction) being 4.1 for the closer object and 6.1 for the more distant one. any ideas why?
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 * (radius - distance);
Debug.Log(obj.name);
Debug.Log(forcemulti*direction);
if (forcemulti < Mathf.Infinity)
{
obj.GetComponent<Rigidbody2D>().AddForce((direction * (forcemulti/Time.deltaTime)), ForceMode2D.Impulse);
}
}
}
}
Yeah, you'll have to use direction.normalized :) direction currently has a length of distance
Its working perfectly now, thank you so much for your help :D
Your answer