Hi.
I did a Faux Gravity script based on Faux Gravity Topic.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class SphereGravity : MonoBehaviour {
#region variables
public float GravityRange = 10f; // the range of the gravity affect
public float gravityFaux = 10f; // the force of the gravity effect
private RaycastHit[] HitObjects; // objects in the gravity range
#endregion
#region methods
void Start() {
rigidbody.constraints = RigidbodyConstraints.FreezeAll;
}
void FixedUpdate () {
// get all the objects in the range of the gravity field
HitObjects = Physics.SphereCastAll(transform.position, GravityRange, Vector3.up);
// for each object that it is inside the range
foreach (RaycastHit hit in HitObjects) {
if (hit.transform.name != "Planet") {
float Distance = Vector3.Distance(transform.position, hit.transform.position);
// get the diference of relative gravity pull
Vector3 attractDirection = transform.position - hit.transform.position;
attractDirection.Normalize();
// now we push the object
hit.rigidbody.AddForce(attractDirection * (gravityFaux / Distance) * hit.rigidbody.mass);
}
}
}
void OnCollisionEnter(Collision c) {
}
#endregion
}
So far it detect my “Bike” GameObject children. What i mean:
i have a Bike GameObject which have 3 other objects (child):
- Body
- Front Wheel
- Back Wheel
Here my doubt:
How to make the Rigidbody on each object in child affect the parent position? So far my script use AddForce to make a rigidbody goes to the center of my “Planet” (is how i call the terrain)
I would like to each object work with your own rigidbody, so I guess this could make an interesting gravity effect to the whole object.
here a picture of my bike:
