• 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 ItsKhanny · Sep 16, 2015 at 12:06 PM · c#

How to make a central point of gravity

I want to make planets that have central points of gravity, but I cannot figure out how to. All I need is to be able to make round game objects have gravity like planets. Please help

Edit:

I am wondering how to make gravity that can put objects in orbit.

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

4 Replies

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

Answer by Nikunj-Kareliya · Sep 16, 2015 at 07:31 PM

Suppose, you have 2 types of objects : Big object & small objects.

Small objects revolves around big object. Add below script on small objects.

alt text

 using UnityEngine;
 using System.Collections;
 
 public class CircularGravity : MonoBehaviour {
     
     public Transform target; // Big object
     Vector3 targetDirection;
 
     public int radius = 5;
     public int forceAmount = 100;
     public float gravity = 0;
     private Rigidbody rb;
 
     private float distance;
 
     void OnDrawGizmos() {
         Gizmos.color = Color.yellow;
         Gizmos.DrawWireSphere(transform.position, radius);
     }
 
     // Use this for initialization
     void Start () {
         Physics.gravity = new Vector3 (0, gravity, 0);
         rb = GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update () {
 
         targetDirection = target.position - transform.position; // Save direction
         distance = targetDirection.magnitude; // Find distance between this object and target object
         targetDirection = targetDirection.normalized; // Normalize target direction vector
 
         if(distance < radius) {
             rb.AddForce (targetDirection * forceAmount * Time.deltaTime);
         }
 
 
     }
 }
 



gizmodraw.png (16.2 kB)
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 Suddoha · Sep 17, 2015 at 11:05 AM 0
Share

Basically the same that i posted. I'm wondering if the OP even tried it out and did not just wait for a more complete version. Good job though, but I'd the AddForce line into FixedUpdate.

avatar image
-1

Answer by sys12 · Sep 16, 2015 at 12:11 PM

Check this out: http://docs.unity3d.com/ScriptReference/Rigidbody-centerOfMass.html

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 sys12 · Sep 16, 2015 at 01:12 PM 0
Share

http://forum.unity3d.com/threads/finding-the-center-of-mass-of-a-collection-of-rigidbodies.87578/

avatar image
0

Answer by Suddoha · Sep 16, 2015 at 12:37 PM

I'm not sure how well sys12's suggestion works for this purpose, but you can do some funny stuff with the physics system. The following is just quickly made out of nowhere, but it's quite cool for orbit-like effects. You can tweak it way more to have your desired behaviour.

You can also code something similar from scratch which doesn't invole the physics system, but just play around with it, maybe it inspires you a little bit:

(disable useGravity on the rigidbody)

 using UnityEngine;
 using System.Collections;
 
 public class Gravity : MonoBehaviour
 {
 
     public GameObject planet;
     private Rigidbody rb;

     [Range(0, 5)]
     public float factor = 1;
     public ForceMode mode;
 
     void Awake()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         Vector3 g = (planet.transform.position - transform.position) * factor;
         rb.AddForce(g, mode);
     }
 }
Comment
Add comment · Show 6 · 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 ItsKhanny · Sep 16, 2015 at 12:55 PM 0
Share

putting in the exact code other than the class name, i got the following errors

error CS0178: Invalid rank specifier: expected ,' or ]'

error CS1519: Unexpected symbol `float' in class, struct, or interface member declaration

avatar image Suddoha ItsKhanny · Sep 16, 2015 at 01:54 PM 0
Share

Oh sorry, probably deleted that by accident as I tested the script so it was definitely there. $$anonymous$$y bad. I'll fix the original post.

avatar image ItsKhanny · Sep 16, 2015 at 01:02 PM 0
Share

It was because you did private RigidBody not private RigidBody = rb

edit: What does the script do exactly

avatar image Suddoha ItsKhanny · Sep 16, 2015 at 01:59 PM 0
Share

Oh btw, don't forget to disable the rigidbodies gravity.

The script takes an object as a gravity target (planet variable). It then calculates the direction from the current objects position to the target and applies a force in this direction via the rigidbody.

The mode determines how the force is applied (acceleration suits well i think), the factor variable can be used to adjust the amount of acceleration.

Without adjustements, the script will probably not work for something like a walk on the planet. That has to be improved but it's just to get you started anyway. ;)

avatar image ItsKhanny · Sep 16, 2015 at 04:05 PM 0
Share

does this script work on making stuff orbit and crash into objects/planets

avatar image Suddoha ItsKhanny · Sep 16, 2015 at 06:02 PM 0
Share

Have you tried to use it? Play around with it, as i said you may have to adjust the one or other thing. i cannot provide a script that totally fits your needs, as I 1) don't know which exact behaviour you're looking for and 2) it's probably too much to ask for on a Q/A site.

avatar image
0

Answer by remi07 · Jan 13 at 03:03 PM

Try the following:

 public class ObjectOnPlanet: MonoBehaviour
 {
 
     private Rigidbody rigid;
    
     void Start()
     {
         rigid = GetComponent<Rigidbody>();
     }
 
     Vector3 planetGravity;
 
     private void FixedUpdate()
     {
         planetGravity = Quaternion.FromToRotation(Vector3.up,transform.position) * Physics.gravity;
         rigid.AddForce(planetGravity,ForceMode.Force);
     }
 
 }

Basically we take the gravity vector and rotate it to align it to the vector that goes from the origin of the scene to the object position (basically transform.position). That particular rotation is deduced by using the Quaternion.FromToRotation(Vector3.up, transform.position) because physics.gravity is by default aligned along Vector3.up in world space.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

31 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges