Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Berretta · Apr 08, 2016 at 04:23 PM · velocityphysics2daddforcepushpull

Choosing speed and direction of AddForce

Hello!

So I'm making a 2D game in which your character has the ability to push things with a Jedi-like force. I've successfully implemented except for one problem: the amount of force (and thus, speed) our character applies on the desired objects is way bigger the farther away they are from each other.

I wish to make it so that the closer an object is to the character, the bigger the force applied is, and the farther away it is, the weaker (so basically, the opposite of what I'm getting).

This is my character's code:

  Vector2 dir = this.transform.position - go[i].transform.position;
    
             float distance = Vector2.Distance (go[i].transform.position, transform.position);
  
             if (porp == 1)
             {
                enemybody.AddForce(dir * (100 - distance));
             }


I inserted that (100 - distance) in hopes of being able to fix it, but alas, 'dir' overpowers it.

Is there a way to use AddForce by a) providing a direction, b) providing the speed in which it should travel that direction?

Thank you!

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 MelvMay · Apr 08, 2016 at 06:09 PM

Sounds like you want the force to be inversely proportional to the distance. Two common functions are inverse-linear and inverse-squared.

A more realistic approach is to use the inverse-squared for forces though.

Here's an example script that should get you going on this:

 using UnityEngine;
 
 public class DistanceCheck : MonoBehaviour
 {
     public Transform Obj1;
     public Transform Obj2;
     public float forceSize = 10.0f;
 
     void Start ()
     {
         Vector2 direction = Obj2.position - Obj1.position;
         float distance = direction.magnitude;
         direction.Normalize ();
 
         var invLinearForce = direction * (forceSize / distance);
         var invSquaredForce = direction * (forceSize / (distance * distance));
     }
 }

f

The above is just an example though. You will have to be careful that you don't use distance = 0 by clamping it to a value above zero. Note that as it approaches zero, especially with the inverse-squared, the force will get very, very large. Simple clamping of either the force or the distance is one way to achieve this.

Comment
Add comment · Show 1 · 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 Berretta · Apr 08, 2016 at 06:14 PM 0
Share

Exactly what I needed, math isn't my strong suit, thank you!!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Determining how much force to add to reach a certain velocity in a certain amount of time and clamping it 0 Answers

Trying to add force to an object... is this done correctly? 0 Answers

How do I create Sling-Shot like propulsion? 1 Answer

RigidBody2D AddForce Not Adding Acceleration 0 Answers

How to speed up an object with AddForce? 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