- Home /
What would the best way be to do directional gravity on a cube?
I am building a system that puts the player movement on a cube, moving in grids. When the player reaches the edge of the cube, the player should be able to rotate and move to the next side. I don't want to rotate the whole cube itself, as there are objects on it, and the player is an independent entity. Based on grid movement, the player currently moves with transform. What would be the best way to handle 'gravity', even simulated under transform, for this kind of system?
Answer by save · Aug 18, 2013 at 09:55 PM
The most convenient way is to create your own physics class and apply those behaviors to your physic objects. Quick example:
class MyPhysics {
static var gravity : Vector3 = Vector3(0,-9.8,0);
}
// A physic object
private var myMass : float = 2.0;
function Update () {
transform.Translate(MyPhysics.gravity*myMass*Time.deltaTime);
}
// Changing gravity
MyPhysics.gravity = Vector3.Lerp(MyPhysics.gravity, Vector3(-9.8,0,0), 10*Time.deltaTime);
If the objects have different physics all together, just create different rules depending on which side of the cube world they're on, defining a local gravity.
enum LocalGravity {
onLeft,
onRight,
onTop,
onBottom
}
var localGravity : LocalGravity = LocalGravity.onTop; //Which would be normal gravity downwards in world perspective
var myMass : float = 1.0;
private var gravityDirection : Vector3;
function Awake () {
switch (localGravity) {
case LocalGravity.onLeft: gravityDirection = Vector3(9.8,0,0); break;
case LocalGravity.onRight: gravityDirection = Vector3(-9.8,0,0); break;
case LocalGravity.onTop: gravityDirection = Vector3(0,-9.8,0); break;
case LocalGravity.onBottom: gravityDirection = Vector3(0,9.8,0); break;
}
}
function Update () {
transform.Translate(gravityDirection*myMass*Time.deltaTime);
}
Answer by Bloodyem · Aug 18, 2013 at 09:50 PM
Well, I can't say this is right, but I'll throw in my two cents.
What I would do is make a collision object, and put it into the grid where the player can move. When the player hits it, use rigidbody.addforce(Vector3(<>), so the player is reacted on by a different gravity. It might not be a bad idea to remove the gravity from the gameobjects rigidbody component, and simply use forces like this in an update or fixedupdate function.
Again, don't know if it'll help.
Your answer
Follow this Question
Related Questions
Character floats upward when looking up and down 0 Answers
Slowly move a GameObject on 1 axis, then destroy it. 1 Answer
Two problems One code 1 Answer
move enemy to old position of player 2 Answers
Helicoper Movement Help 1 Answer