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
1
Question by yade_123 · Oct 17, 2015 at 04:08 PM · c#2dgravity

HOW TO ADD GRAVITY TO A OBJECT?

Hi everyone I wanted to add gravity to a object but not the way it is usually done. I want to add gravity to my object A that affects object B like stars and planets, where the planets are pulled by the gravitational force of the star.So if object B is too close to object A while moving it will collide with it, but if it is at a proper distance then it will be affected by the gravity but it will not collide , only change its direction.THE IMAGE BELOW WILL MAKE IT EASY TO UNDERSTAND WHAT I WANT. So I have no idea how to make this possible, please help me out. NOTE It is a 2d game. thank youalt text

gravity.png (9.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Jessespike · Oct 17, 2015 at 04:57 PM

Implement Newton's law of universal gravitation. Wikipedia has a lot of jargon, You can probably find a simpler explanation somewhere else:

Force (F) of gravitational attraction between two objects with Mass1 and Mass2 at distance D is:

  F = G(mass1*mass2)/D squared.

  (G is the gravitational constant, which has the same value throughout our universe.) 

So basically, create a script for your objects that have a Mass and a Velocity. And have another script that will use the formula to calculate the relative forces, you just then update the object's velocity based on the calculated force and direction.

Edit ---

If you're looking for a simpler solution, just for the example that you've provvided. You can probably just have 1 object move towards a point and if the objects get too close, then you change the target that the object moves towards. Psuedo example:

  if (Vector3.Distance(star.transform.position, planet.transform.position) < 1f)
  {
       // objects are within proximity
       targetPosition = star.transform.position; // set targetPosition to the star
  }
  else
  {
       // objects are not close enough to each other
       targetPosition = someOtherPosition; // set targetPosition to some reference
  }
 
  planet.transform.position = Vector3.MoveTowards(planet.transform.position, targetPosition);
         
     

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
1

Answer by bubzy · Oct 17, 2015 at 07:24 PM

well, this kinda works, maybe you can tweak it. (its for 3d sorry but the principle is the same)

 using UnityEngine;
 using System.Collections;
 
 public class gravityTest : MonoBehaviour {
 
     // Use this for initialization
     public float influence;
     public float mass;
     public bool isPlayable = false;
     float speed = 1; //not sure how you will find this out, it may be easier in 2d?
     Rigidbody body;
 
     void Start () {
         body = gameObject.GetComponent<Rigidbody> ();
     }
     
     // Update is called once per frame
     void Update () {
         if (isPlayable) { //this is for testing purposes however you control the object is up to you.
             if (Input.GetKeyDown (KeyCode.A)) {
                 body.AddForce (new Vector3 (0, 10f, 0));
             }
         }
 
         Collider[] hitColliders = Physics.OverlapSphere(transform.position, influence);
         int i = 0;
         while (i < hitColliders.Length) {
             Debug.Log(hitColliders[i].name);
             if(hitColliders[i].tag == "planet")
             {
                 gravityTest test;
                 test = hitColliders[i].gameObject.GetComponent<gravityTest>();
                 if(test.mass > mass)
                 {
                     body.AddForce((hitColliders[i].transform.position - transform.position) * speed * Time.smoothDeltaTime);
                 }
             }
             i++;
         }
     
     }
 }

this simply attracts an object to another, the one with the smaller mass being attracted to the larger one, you have to tag the objects "planet". see how you get on with it :)

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 yade_123 · Oct 19, 2015 at 03:55 PM 0
Share

Thank you, it did work , but not in the way I expected. I used it in 3d , so it does move upward and gets influenced by the gravity. but there is no change in the direction as shown in the figure above.Any idea how I can bring in that change.

avatar image bubzy yade_123 · Oct 21, 2015 at 02:10 PM 0
Share

fiddle with the numbers :) it does function like your diagram under certain conditions. (this is an approximation of the real physics though, im way too dumb to understand how it actually works)

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

39 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

Related Questions

How To Do Basic 2D Movement? 1 Answer

How to move the player only 1 tile per buttonPress? 2 Answers

Making a list of images within a scrollrect generated from prefabs tappable/clickable 1 Answer

Drag 2D Sprite by Touch 1 Answer

Collision between two prefabs not being detected 0 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