Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mayero · Feb 01 at 10:30 PM · force

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));
             }
         }
     }
 }
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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/

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mayero · Feb 02 at 08:16 PM 0
Share

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);
             }
         }
     }
 }
 
 

avatar image olejuer Mayero · Feb 02 at 09:37 PM 1
Share

Yeah, you'll have to use direction.normalized :) direction currently has a length of distance

avatar image Mayero olejuer · Feb 02 at 10:06 PM 0
Share

Its working perfectly now, thank you so much for your help :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ForceMode.Impulse Doesnt always fire. 2 Answers

Gravity is preventing object from moving straight toward its direction when calling rigidbody.AddForce... 1 Answer

Help with player movement and adding force to a ball. 1 Answer

How can i set AddRelativeForce relative to how fast/slow im moving my mouse? 2 Answers

Force input on Button? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges