- Home /
Duplicate Question
Spherical Gravity
How would I go about adding gravity to a sphere(planet) I am also wanting to add terrain to the planet in the future so how would I go about doing this? I am fairly new to scripting in unity, so any help would be fantastic!
Word of warning- do not expect people to answer questions that consist of a very long title, no body, and virually useless tags. I've cleaned this one up for you, and clunk has very kindly chosen to answer you anyway, but in future please try to ask more clearly formatted questions.
There are WAY TOO $$anonymous$$ANY IDENTICAL QUESTIONS on this. $$anonymous$$oderators, you are allowed to use the "close" button.
Video tutorial for spherical gravity: https://www.youtube.com/watch?v=UeqfHkfPNh4
Answer by clunk47 · Sep 23, 2013 at 03:29 AM
Well, you need to ask one question at a time here. You also need to be a bit more specific on your needs. I'm not sure what you're asking as far as adding gravity to a sphere. If you're talking about adding a gravitational "pull" to a sphere, like a "planet", you could try something as simple as placing all the rigidbodies out in "space" into an array or list. Then in a for statement, get the distance between your planet and each body out in space. Make a range, in this example is 100, and have the planet pull those physical bodies toward it if they are close enough. I won't get into anything to detailed, or smooth functioning, because you weren't in any way specific. Try this out, break down the code, and look up each piece of it in the Unity Script Reference if you are not sure what it means. As for your terrain question, ask it as a separate question after you work this one out.
//Example.cs
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
Object[] RBs;
float range = 100.0f;
float stopDistance = 10.0f;
void Awake()
{
RBs = GameObject.FindObjectsOfType (typeof(Rigidbody));
}
void Update()
{
foreach(Rigidbody RB in RBs)
{
float dist = Vector3.Distance(RB.transform.position, transform.position);
if(dist < range && dist > stopDistance)
{
RB.position = Vector3.MoveTowards(RB.position, transform.position, Vector3.Distance(RB.transform.position, transform.position) * Time.deltaTime);
}
}
}
}
Ahh, thanks for this reference, definitely something I will bookmark, because I'm still learning. :)
Didn't want to get too into detail in case this isn't what the PO is looking for, so made up a quick, clunky but functional example. +1 for the great wiki link, very useful information.
If you want your feet on the ground, you will also need to worry about rotating your stuff, aligning their 'up' directions with 'out' from the center of the planet.
@criddle98 - Someone deleted your question originally, because it was vague, even with the title being too long, tags were not really a help either. But I undeleted it because I could see you really needed help. All the information provided should at least get you started, thanks to Syclamoth and DaveA, you have more to go off of than my simple answer alone. Definitely give up-votes to these guys for taking time to help resolve your issue.
I actually answered a similar question (pertaining to the 'feet-down' problem) earlier today- Rotate to Orient to Sphere Surface