I’ve been looking to a solution for the last 14 hours.
I’ve explored the faux gravity option, but none of the scripts are working for me. So now the only option I can think of is to have the world rotate as my FPController moves.
I’m not entirely sure what you’re asking, but if you have a character on a sphere it seems as though you could apply gravity manually by attaching a script like this to each player and enemy:
public var go_earth : GameObject; //drag the planet into this variable in the Inspector
public var fl_gravity : float = 20; //amount of gravity to apply
function FixedUpdate() {
var v3_gravity_direction : Vector3;
v3_gravity_direction = transform.position - go_earth.transform.position;
v3_gravity_direction = v3_gravity_direction.normalized * fl_gravity amount;
go_earth.GetComponent(CharacterController).move(v3_gravity_direction);
}
The gravity will NOT be an acceleration, like real gravity is. It’ll be a constant speed. And this assumes there’s a Character Controller attached to the player/enemy/whatever.
And another disclaimer: this is just off the top of my head and has not been tested. At all.
Sorry to interrupt this great topic but I just wanted to know if this topic can be an answer for a small spherical game environment. The center of the sphere is the origin of gravity and player and enemies can wander around the sphere just like a small planet. This is what I was trying to do instead of a planar game map.
I would be so happy to learn the solution for that. But at the same time I don’t want to hi-jack this topic. So if my situation is not related to the main question, then please just ignore it and I will create another topic after I complete my research.
public var go_earth : GameObject; //drag the planet into this variable in the Inspector
public var fl_gravity : float = 20; //amount of gravity to apply
function FixedUpdate()
{
var v3_gravity_direction : Vector3 = - transform.position + go_earth.transform.position;
v3_gravity_direction = v3_gravity_direction.normalized * fl_gravity;
var controller : CharacterController = GetComponent(CharacterController);
controller.Move(v3_gravity_direction);
}
@script RequireComponent(CharacterController)
Brett’s code would move the sphere towards the object that had this script instead of moving the object towards the sphere…
Don’t worry, it’s not hijacking. it’s essentially what I’m after as well.
The script is making my character stick to the sides of the sphere! So something’s working. The problem now is that the character is literally stuck and cannot move. I’ve tried placing it in various different starting positions but I’m having the same problem everywhere.
This is a college project, what I’ve done is exported the game world from the Indie game Love and I’m using that mesh as the basis. It probably takes about 10 to 15 mins to walk around the entire world in the normal game speed.
You can’t use a Character Controller for a planet walker because the Character Controller always remains upright. I would recommend using a rigidbody with a capsule collider to obtain the same feeling.
Also, gravity is causes an object to accelerate. That’s why the longer an object falls, the faster it goes. An object falling doesn’t fall at a constant 9.8 m/sec. If that were the case, falling from 1 meter and falling from 50 meters, you would land at the same velocity which isn’t correct.
To be attracted with a rigidbody all’s you have to do is this Its essentially the same as below but with rigidbody.AddForce() instead of Controller.Move. The only thing you have to figure out is the rotation of the object.f
var attractionForce : float = 9.8;
var attractor : Transform;
function FixedUpdate () {
var gravityDir : Vector3 = attractor.position - transform.position;
gravityDir = gravityDir.normalized;
rigidbody.AddForce(gravityDir * attractionForce);
}
edit, This would pull the player to the planet. I would go with your original idea of moving the player around the planet. Maybe we can help you with that script. Sorry I read your first post wrong when I wrote this :(.