- Home /
Duplicate Question
How to use gravity around sphere?
How can I place a planetary gravity around the sphere such that all objects on sphere such as players and enemies do not fall. They remain attached to the sphere.
Without thinking about this too deeply a quick solution would be to turn off the built in Gravitational force (set it to [0,0,0]), then keep a list of all relevant GameObjects and set the constantForce for each object in every FixedUpdate based on the direction from the game object to the center of your planet. If you just need things to stick to the surface, or if all objects will be approximately the same distance from the planet you can use a single gravitational force magnitude for all objects. If you need real planetary gravity just solve the gravitational force equation for each object (F=G*m1*m2/r*r).
Google: "unity3d planet gravity." Lots of hits, lots of info. The questions has been answered many times, plus I believe you will find some tutorials.
my EX script for gravity:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlanetGravity : $$anonymous$$onoBehaviour {
public float GravityStrength = 10;
public List<Transform> AddGravityToObject = new List<Transform>();
public GroundDetection GroundDecScript;
// all that enter Planetary gravitational point are forced to gravity
void OnTriggerEnter(Collider other){
if (other.rigidbody != null){
AddGravityToObject.Add(other.transform);
}
}
void OnTriggerExit(Collider other){
AddGravityToObject.Remove(other.transform);
}
void FixedUpdate (){
// for all objects we are pulling
for (int i=0; i<AddGravityToObject.Count; i++) {
// we load ground detection script
GroundDecScript = AddGravityToObject[i].GetComponent("GroundDetection") as GroundDetection;
// so that force is weaker far site of the planet // weaker force works but not direction don't know why, ...
// I have to check distance with ray and multiply acordingly to it otherwise it won't work correctly
// Temp = transform.position - AddGravityToObject[i].position;
// if (Temp.x > 1) {Temp.x = 160;} else if ((Temp.x < -1)) {Temp.x = -160;}
// if (Temp.y > 1) {Temp.y = 160;} else if ((Temp.y < -1)) {Temp.y = -160;}
// if (Temp.z > 1) {Temp.z = 160;} else if ((Temp.z < -1)) {Temp.z = -160;}
// Debug.Log(transform.position - AddGravity[i].transform.position); // distance from planet
// if (GroundDecScript != null && ! GroundDecScript.Grounded){
AddGravityToObject[i].transform.position = Vector3.$$anonymous$$oveTowards(AddGravityToObject[i].transform.position, transform.position, 0.075f);
//rigidbody.AddForce( (transform.position - AddGravityToObject[i].transform.position) * GravityStrength * Time.deltaTime);
// }
// else if (GroundDecScript == null){
// Debug.Log("Does work BUT add the ground detection to object Hiearchy name: " + AddGravityToObject[i].name);
// AddGravityToObject[i].rigidbody.AddForce( (transform.position - AddGravityToObject[i].transform.position) * GravityStrength * Time.deltaTime);
// }
}
}
}
use what you can :)
OFC if this is what you needed please accept my answer :)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Movement problem? 0 Answers
Double Jump 1 Answer
Sphere rotation in moving direction 1 Answer
How can i make artificial gravity for Kinematic Rigidbody? 3 Answers