Hi,
I’m trying to use the current faux gravity methods from:
http://forum.unity3d.com/threads/8873-Faux-Gravity-making-my-brain-spin…-Help!
http://forum.unity3d.com/threads/33987-Character-align-to-surface-normal
It works, to a degree. The problems I’m having are:
- Wanting to use a rigidbody as the player, rather than character controller
Kind of works, but stops a little halfway below the planet
- Allowing the player to jump onto obstacles, such as platforms etc - without being completely restrained to only the sphere, which it currently is with those scripts.
The best working examples of this would be from Ratchet Clank 2 (I believe it was 2 - going commando / locked and loaded) which had a moon planet that you could run around and still jump on other platforms as well as “jump grids”.
The way “1” works is by using the following script from page 2 of this thread:
http://forum.unity3d.com/threads/33987-Character-align-to-surface-normal
var rotationSpeed = 120.0;
var translationSpeed = 10.0;
var height = 7.2; //height from ground level
private var centre : Transform; //transform for planet
private var radius : float; //calculated radius from collider
var planet : SphereCollider ; //collider for planet
function Start ()
{
//consider scale applied to planet transform (assuming uniform, just pick one)
radius = planet.radius * planet.transform.localScale.y;
centre = planet.transform;
//starting position at north pole
transform.position = centre.position + Vector3(0,radius+height,0);
}
function Update ()
{
//translate based on input
//var inputMag = Input.GetAxis("Vertical")*translationSpeed*Time.deltaTime;
//transform.position += transform.forward * inputMag;
//snap position to radius + height (could also use raycasts)
targetPosition = transform.position - centre.position;
var ratio = (radius + height) / targetPosition.magnitude;
targetPosition.Scale(Vector3(ratio, ratio, ratio) );
transform.position = targetPosition + centre.position;
//calculate planet surface normal
surfaceNormal = transform.position - centre.position;
// surfaceNormal.Normalize();
//GameObject's heading
//var headingDeltaAngle = Input.GetAxis("Horizontal") * Time.deltaTime * rotationSpeed;
//headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
//align with surface normal
//transform.rotation = Quaternion.FromToRotation( transform.up, surfaceNormal) * transform.rotation;
//apply heading rotation
//transform.rotation = headingDelta * transform.rotation;
}
I am obviously not using it’s rotational or movement properties.
Instead, I then have a character with the following movement code a rigidbody w/ gravity enabled:
function FixedUpdate() {
if (Input.GetKey ("w")) {
rigidbody.AddForce(forward * speed * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey("s")) {
rigidbody.AddForce(forward * -speed * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey ("d")) {
rigidbody.AddForce(right * speed * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey ("a")) {
rigidbody.AddForce(right * -speed * Time.deltaTime, ForceMode.VelocityChange);
}
}
function Update() {
var hit: RaycastHit; // use raycast to check if character grounded (3.5m tolerance)
var castPos = Vector3(transform.position.x,transform.position.y-.25,transform.position.z);
if (Physics.Raycast (castPos, -transform.up, hit)) {
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
}
}
I figured I might be able to make an array of obstacles you could jump onto and move on - but then won’t that stuff up the gravitational pull?
Any insight onto the right path?

