- Home /
planetary gravity: falling "off"
now, my player stays within the vicinity of the planet, and when it gets below or above the global y axis (below an altitude of 0), the gravity for the player shifts so that instead of gravity pushing down, it pushes you up.
but you can't move the player at all, so you're stuck shifting between the two gravities, and unable top get back on land....
if there's a better way to make a mario style planet physics, where it looks like the planet rotates, but you the player or gravity itself changes so that the planet is always directly under you no matter where you go on the planet... then couold you give me a hint on how to script that? thanks.
here's my current script;
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 10.0; private var moveDirection = Vector3.zero; private var grounded : boolean = false; var MoveDirection : Vector3;
function Update() { my=moveDirection.y;
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection *= speed;
} else {
moveDirection = transform.position.normalized * gravity * Time.deltaTime;
moveDirection.y=my;
}
if (Input.GetKeyDown ("space")) {
moveDirection.y = jumpSpeed;
}
if(transform.position.y >= 0)
{
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
}
if(transform.position.y <= 0)
{
moveDirection.y += gravity * Time.deltaTime;
}
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
Answer by GlitchEnzo2 · Dec 04, 2010 at 05:36 PM
A much better way of doing it is to calculate the actual vector between the player and the planet and use that as the gravity vector.
If your planet is positioned at the origin, then all you have to do is normalize the player position:
moveDirection = transform.position.normalized * gravity * Time.deltaTime;
If it's not at the origin, you just have an additional subtraction:
moveDirection = (transform.position - planetCenter).normalized * gravity * Time.deltaTime;
Update - To help explain, here is a simple example:
1) Create a sphere at (0, 0, 0)
2) Scale the sphere to (10, 10, 10)
3) Create a cube at (0, 0, -15) and give it a RigidBody component
4) Disable the gravity on the cube.
5) Apply the following script to the cube
using UnityEngine; public class SphericalGravity : MonoBehaviour { float gravity = -9.8f;
void Update ()
{
rigidbody.velocity += transform.position.normalized * gravity * Time.deltaTime;
}
}
When you run the game, you should see the cube fall toward the sphere and then land on its surface.
actually, i tried it, and it's still not working.... i'm guessing we're working with my FPFlyer script since that's the script that controls my movement, so i'll post my updated progress....
Could you elaborate a bit on how it's not working? $$anonymous$$aybe then I could help a little more.
i'm not sure what you gave me is supposed to do, but i think i know what i need: some code that makes the center of gravity (the point everything goes toward) Vector3.zero, or the origin, since that's the center of my planet. but i'm not sure how to do this...
I editing my answer to give you a more detailed example. Hopefully it makes things clearer for you.
Answer by Jesus_Freak · Dec 04, 2010 at 04:38 PM
the correct way to do the equator 'shift' is by the code that says:
if(transform.position.y >= 0)
{
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
}
if(transform.position.y <= 0)
{
moveDirection.y += gravity * Time.deltaTime;
}
in the FPFlyer script.
There is a couple of things wrong with this code. Firstly it is only in one dimension, unless your planet is a plane this code certainly acheive the desired result. Secondly it is constant speed where as gravity is applies acceleration. Change this code to what GlitchEnzo has.
Answer by greg horn · Dec 19, 2010 at 04:43 PM
I am currently trying to achieve exactly the effect discussed here... I have a first person view character and he should be able to walk on a sphere without falling off, and the ground should always appear below him. I'm new to scripting though and haven't been able to implement GlitchEnzos script yet on my First Person Controller. Can anyone provide a step by step walkthrough on how to do this?