- Home /
Shifting gravity from the ground to the wall.
First off I'm not wanting anyone to actually make the code for me. Just a nudge in the right direction is what I'm hoping for.
Now for the actual question. I have a cube serving as the "world" and what I'm trying to do is get the player to be able to jump or move to the edge of the cube and then have its gravity shift 90 degrees to be on the new surface of the cube. Another example could be jumping from the floor and then ending up on the wall shifting the gravity from the floor to the wall, which now serves as the floor. Very similar to some moments in Psychonauts.
Now I'm trying to figure how to go about this. Which method I should be using. Shifting the entire player's gravity orientation? Then I'd need to do something else with the player's positional orientation. Or rotate the entire world? Rotating the world seems way too much. I'm using a character controller by the way. But I'm not really sure where I should start.
If anyone knows how to go about starting this suggestions will be very appreciated. Thank you.
Answer by Waz · Aug 23, 2011 at 12:09 PM
You cannot use built-in gravity, it is Y only. However, built-in gravity is just a simple constant force, so you can make your own with ConstantForce.
You'll want to rotate the controller, and be very careful with your handling of direction - most existing controller scripts have simplistic world cordite space logic the will not work in your case.
Also be careful with the various Look functions that take a default Up vector of +Y, as you probably want something else.
You could also rotate the world, but that could be costly if it's many static colliders.
So I rotate the player and then the character controller separate to meet up with the player's new orientation yes? I got that to work more or less just now I have to figure how to switch the gravity direction relative to the world so that it doesn't go wacky on me. I'm not using a rigidbody by the way. I hard coded everything in. Thanks for the reply.
Found out I can't rotate a character controller because its Y axis is always Y so walking on walls type of deal is going to have to be looked at in a completely different way somehow. Thank you though.
Had to end up using a Rigidbody. $$anonymous$$ore or less it works the same. But at least it allows me to shift gravity from the floor to the wall. Just have to work with the camera and a few other things to compensate for the new orientation but at least I'm making progress. Should I post what I've come up with to make it rotate as an answer or no?
Answer by dre38w · Oct 04, 2011 at 01:19 AM
Okay here is the gravity shifting part. The actual controls and camera situation is still a work in progress so it's not fully functional.
On Player.
if (!RotateTriggers.canRotate && !isOnWall)
{
rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
}
if (RotateTriggers.canRotate)
{
Quaternion localRot = Quaternion.FromToRotation(-transform.up, Vector3.right) * transform.rotation;
transform.rotation = Quaternion.RotateTowards(transform.rotation, localRot, 10);
rigidbody.AddForce(new Vector3(gravity * rigidbody.mass, 0, 0));
}
if (RotateTriggersWall.canRotateToWall)
{
if (Input.GetButtonDown("Jump"))
isOnWall = true;
}
if (isOnWall)
{
Quaternion localRot = Quaternion.FromToRotation(-transform.up, Vector3.right) * transform.rotation;
transform.rotation = Quaternion.RotateTowards(transform.rotation, localRot, 10);
rigidbody.AddForce(new Vector3(gravity * rigidbody.mass, 0, 0));
}
}
On the trigger that the player would collide with when they fall off a ledge.
public static bool canRotate = false;
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Player")
canRotate = true;
}
And on the trigger near a wall.
public static bool canRotateToWall = false;
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Player")
canRotateToWall = true;
}
Keep in mind this is just for one direction. But the other directions should work quite similar. I'm not sure if this is the best or the most efficient way to do it but it works for the gravity shifting portion.
Your answer
Follow this Question
Related Questions
Gravity for my AI 0 Answers
How reliable are isGrounded checks? 1 Answer
Rpg style movement help. 2 Answers