Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
6
Question by DubstepDragon · Dec 19, 2013 at 11:35 AM · c#gamegravity

3D Gravity towards one object

I am already aware of unity's directional gravity from the world properties, and have been experimenting quite a bit with it recently... However, I wanted an object to attract all objects in a specific radius (trigger?) around it. Basically, it is a gun that fires a small GameObject that will pull everything in area towards it... Is it possible?

Thanks in advance!

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

3 Replies

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

Answer by Tomer-Barkan · Dec 19, 2013 at 11:54 AM

Assuming all the objects that need to be pulled have rigidbodies (and colliders), you can use Physics.OverlapSphere to detect all the objects within a radius, and then manually apply the force using Rigidbody.AddForce.

 public class ObjectPuller {
     public float pullRadius = 2;
     public float pullForce = 1;
 
     public void FixedUpdate() {
         foreach (Collider collider in Physics.OverlapSphere(transform.position, pullRadius) {
             // calculate direction from target to me
             Vector3 forceDirection = transform.position - collider.transform.position;
 
             // apply force on target towards me
             collider.rigidbody.AddForce(forceDirection.normalized * pullForce * Time.fixedDeltaTime);
         }
     }
 }


Comment
Add comment · Show 5 · 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 sdgd · Dec 19, 2013 at 12:00 PM 0
Share

Interesting to know

does Physics.OverlapSphere create doubles? like collider triggers?

avatar image Tomer-Barkan · Dec 19, 2013 at 12:06 PM 0
Share

Physics.OverlapSphere simply returns an array of all the colliders within range. If an object has two colliders it might return them both, but I don't think that is possible.

avatar image sdgd · Dec 19, 2013 at 12:19 PM 0
Share

I mean if it's creating doubles like colliders

as if you have too many colliders together you get error doubles.

and it says new doubles are ignored.

avatar image Tomer-Barkan · Dec 19, 2013 at 12:23 PM 0
Share

I never heard of any such problem. Each collider will be returned once regardless of its distance from other colliders.

avatar image supermegamestre · Dec 14, 2017 at 05:42 PM 0
Share

this code not work

avatar image
7

Answer by thk123 · Dec 19, 2013 at 11:49 AM

Perfectly possible, but not using unity's simple gravity property, which is designed explicitly for the case of pulling stuff in one, fixed, direction.

Instead, you need to apply a force to all objects you want to attract. You can do this in a number of ways, but one way is to attach a attracted behaviour to each object you want to pull towards. It could look something like this:

 class Attracted : MonoBehaviour
 {
     public GameObject attractedTo;
     public float strengthOfAttraction = 5.0f;

     void Start {}

     void Update
     {
         Vector3 direction = attractedTo.transform.position - transform.position;
         rigidBody.AddForce(strengthOfAttraction * direction);
 
     }
 }

Making sure, when you attach it to an object, you set the attractedTo to be the object that you want to attract them to.

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 grandtheftaural · Dec 12, 2014 at 06:00 AM 2
Share

The above script by @thk123 worked great. Thank you very much! Now here's a slightly updated version for people having trouble with it, adding gameObject to rigidbody and a FixedUpdate ins$$anonymous$$d of an Update. This is the version working best for me:

c# script below named "AttractedToObject.cs"

 using UnityEngine;
 using System.Collections;
 
 public class AttractedToObject : $$anonymous$$onoBehaviour
 {
 
         public GameObject attractedTo;
         public float strengthOfAttraction = 5.0f;
     
         void Start ()
         {
         }
     
         void FixedUpdate ()
         {
                 Vector3 direction = attractedTo.transform.position - transform.position;
                 gameObject.rigidbody.AddForce (strengthOfAttraction * direction);
         
         }
 }



avatar image MikeBru · Mar 27, 2016 at 12:21 AM 0
Share

gameobject.rigibody is no longer used so you need to use gameObject.GetComponent().AddForce (strengthOfAttraction * direction);

Besides that it is perfect!

avatar image jacobre · Jul 23, 2016 at 03:23 PM 0
Share

Hi, your script is running great. But i want to move around this "attraction object" (my player). $$anonymous$$y Player is a prefab and i set other cubes to be attractedTo Player prefab. the problem is that cubes are always attracted to starting position of the players prefab(which is then change later in game by WASD). do anyone know how to fix that. Thanks.

avatar image
-1

Answer by tiagorpg · Jul 15, 2018 at 12:35 AM

i did this, but to a different need, i created a planet, but now i need to make the ship remain upwards related to the center of the planet, how to do this withouth change the y orientation of the ship ?

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.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Changing gravity direction... 5 Answers

Errors with gravity switching... 1 Answer

Random does not contain a defintion for Range 1 Answer

How do you make image links in the Main Menu light up/glow when hovering? 1 Answer

Using "ouside" functions inside Unity? 2 Answers


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