- Home /
Player Controls Upside Down
public class Gravity : MonoBehaviour
{
private float g = -9.81f;
private bool gravity = true;
Character player;
GameObject avatar;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Character>();
avatar = GameObject.Find("Rainboy");
}
// Update is called once per frame
void Update()
{
Physics.gravity = new Vector3(0, g, 0);
if(Input.GetKeyDown(KeyCode.G)){
if(gravity)
{
gravity = false;
print(Physics.gravity);
}
else if(!gravity)
{
gravity = true;
print(Physics.gravity);
}
}
print(g);
if(!gravity)
{
g = 9.81f;
player.gravity = false;
//player.chara.position.y += 1.5f;
//pos.y = 1.5f;
//Physics.gravity = new Vector3(0, 9.81f, 0);
}
else if(gravity)
{
//player.rig.velocity = new Vector3(player.x,player.rig.velocity.y,player.z);
player.gravity = true;
g = -9.81f;
//Physics.gravity = new Vector3(0, -9.81f, 0);
}
}
void OnTriggerEnter(Collider other)
{
Physics.gravity = new Vector3(0, g, 0);
if(gravity){
gravity = false;
}
else{
gravity = true;
}
}
public class Character : MonoBehaviour
{
[HideInInspector]
public float x;
[HideInInspector]
public float y;
[HideInInspector]
public float z;
private bool dead = false;
private float angle = 0;
void Update()
{
if(!dead){
x = Input.GetAxis("Horizontal") * moveSpeed;
z = Input.GetAxis("Vertical") * moveSpeed;
y = Input.GetAxis("Jump") * moveSpeed;
}
if(!dead)
{
rig.velocity = new Vector3(x,rig.velocity.y,z);
}
else
{
rig.velocity = new Vector3(0,0,0);
}
Vector3 vel = rig.velocity;
vel.y = 0;
if(vel.x != 0 || vel.z != 0){
transform.forward = vel;
}
if(gravity)
{
StartCoroutine(GDelay());
}
else if(!gravity)
{
StartCoroutine(ZeroGDelay());
}
if(jump == true){
if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true){
isGrounded = false;
if(gravity){
rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
else if(!gravity)
{
rig.AddForce(Vector3.down * jumpForce, ForceMode.Impulse);
}
//AudioListener.volume = 1.0f;
if(on == true){
jumpSound.mute = false;
jumpSound.Play();
}
}
}
}
public IEnumerator ZeroGDelay(){
yield return new WaitForSeconds(0.5f);
angle = 180;
}
public IEnumerator GDelay(){
yield return new WaitForSeconds(0.5f);
angle = 0;
}
}
The rest is pretty self-explanatory. I want to be my character walk around upside down, but everytime they move it flips them right-side up
sorry but no this is not "self-explanatory" imo. you post 150 lines of code and a half a sentence describing your problem... What does not work? What have you tried?
as a rule of thumb: if you post more than 50 lines of code you should break the issue further down. If you are not able to do that how should others know what to do?
Ok, here's the basics. So when my character changes gravity, I program it so that he will flip 180 degrees on the "z" rotation. But whenever he moves while upside down, his "z" rotation reverts back to 0, making him collide with the platform and eventually going of the platform and flying off. I realized that the problem lied in Line 96, so I created a bool for that, and he does move upside down, but that just brings up another problem with him not flipping back to 0 degrees on the "z" axis. I also made the character jump down while he's upside down, but whenever he reverts back to normal gravity, he still jumps down, even though I created a bool to tell him otherwise.
I hope this is understandable for you
Your answer
![](https://koobas.hobune.stream/wayback/20220613045230im_/https://answers.unity.com/themes/thub/images/avi.jpg)