• 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
2
Question by MobinS · Oct 11, 2011 at 10:23 AM · force

adding force toward specific object

hi... this is my code :

 rigidbody.AddForce(transform.forward * 1600);

I use this to add force that ball will go forward... but I want that add force to the the ball that it moves toward specific object... how can I do that?

Comment
Add comment · Show 5
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 markpollard · Oct 11, 2011 at 10:41 AM 0
Share

Can u elaborate a bit more on this please.

avatar image syclamoth · Oct 11, 2011 at 10:44 AM 4
Share
 rigidbody.AddForce((whateverObject.transform.position - transform.position) * someFactor * Time.smoothDeltaTime);

There you go!

avatar image MobinS · Oct 11, 2011 at 05:12 PM 0
Share

thnx it worked... but the amount of force change if it is farther... what should I do if I want that same force in any distance?

avatar image sdgd · Aug 15, 2013 at 04:18 PM 0
Share

thanks @syclamoth

exactly what I was looking for.

tho answer is wrong for my needs as if all players on planet look at planet weird things will occur, ...

I modified the script so that force is weaker on the outside of the planet

here it is:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlanetGravity : $$anonymous$$onoBehaviour {
     public List<Rigidbody> AddGravity;
     private Vector3 Temp;
     // all that enter Planetary gravitational point are forced to gravity
     void OnTriggerEnter(Collider other){
         AddGravity.Add(other.rigidbody);
 //        other.rigidbody.AddForce((transform.position - other.transform.position) * 1 * 1/*Time.smoothDeltaTime*/);
     }
     void Update (){
         
         for (int i=0; i<AddGravity.Count; i++) {
             // so that force is weaker far site of the planet
             Temp = transform.position - AddGravity[i].transform.position;
             if (Temp.x > 1)    {Temp.x = 150;}        else if ((Temp.x < -1))    {Temp.x = -150;}
             if (Temp.y > 1)    {Temp.y = 150;}        else if ((Temp.y < -1))    {Temp.y = -150;}
             if (Temp.z > 1)    {Temp.z = 150;}        else if ((Temp.z < -1))    {Temp.z = -150;}
 //            Debug.Log(transform.position - AddGravity[i].transform.position); // distance from planet
             AddGravity[i].AddForce((transform.position - AddGravity[i].transform.position + Temp)*1*Time.deltaTime/*planetary size*/);
         }
         
     }
 }

sorry about if - s they are not in general writing type but it's easier to read for me.

I did it in update ins$$anonymous$$d of fixed update because I think this does not heaving an very important role, ...

avatar image robertbu · Aug 15, 2013 at 04:47 PM 0
Share

@sdgd - open a new question since the situation here appears to be different from your situation.

5 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by ProjectEW · Aug 15, 2013 at 04:49 PM

If you want the force to remain constant regardless of distance, use

 rigidbody.AddForce((otherObject.transform.position - transform.position).normalized * forceAmount * Time.smoothDeltaTime)
Comment
Add comment · Show 2 · 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 SoshJam · Oct 03, 2018 at 01:05 PM 1
Share

It didn't work for me, it doesn't move. Would it still work for a Rigidbody2D?

avatar image Coulomb1 SoshJam · Jul 13, 2021 at 03:16 AM 0
Share

You just need to increment forceAmount a lot.

avatar image
1

Answer by BigBGros · Oct 11, 2011 at 12:30 PM

Hi!

You can use transform.LookAt function to take rotation, then apply the force extracting vector from that rotate acquired, or simply rotate de ball with transform.LookAt and then apply forward force. You can see a good example in SmoothFollow script from Standard assets, perhaps may help you.

BR

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 MobinS · Oct 11, 2011 at 02:52 PM 0
Share

thnx... but in that way the ball became heavy and don't react well in physic reaction... I just want to give force to a direction. what should I do?

avatar image
1

Answer by mlnczk · Nov 21, 2018 at 08:40 AM

 public Transform objectToFollow;
 private Rigidbody rb;
 
 void Start(){
    rb = GetComponent<Rigidbody>();
 }
 
 void Update(){
    if(objectToFollow != null && Input.GetMouseButtonDown(0)){
        rb.AddForce(objectToFollow.forward * 1600f);
   }
 }
Comment
Add comment · 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
0

Answer by Ali-Nagori · Oct 01, 2014 at 10:26 AM

alternative method which i prefer.

      transform.LookAt(target);
      rigidbody.AddRelativeForce(0, 0, power);





Comment
Add comment · 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
0

Answer by LouishB · Nov 21, 2018 at 03:43 AM

I ended up using:

 void ACollisionBlackHole::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
     // Fill set (unique)
     TSet<UPrimitiveComponent*> OverlapSet;
     SphereComp->GetOverlappingComponents(OverlapSet);
 
     for (UPrimitiveComponent* OtherOverlap : OverlapSet)
     {
         //UE_LOG(LogClass, Warning, TEXT("Blackhole : %s"), *OtherOverlap->GetName());
         FVector OverlapPosition = OtherOverlap->GetComponentTransform().GetLocation();
         FVector Direction = (GetActorLocation() - OverlapPosition).GetSafeNormal() * OtherOverlap->GetBodyInstance()->GetBodyMass() * DeltaTime * 100000;
         OtherOverlap->AddForce(Direction);
     }
 }

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Destructible objects by a projectile 1 Answer

Rigidbody force problem 1 Answer

How to Move Player in Particular Direction Without holding Down a Button? 0 Answers

Simple Rigidbody / force question 3 Answers

How to Instantiate a Gameobject with force. Please Help 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