- Home /
Why can't I move along the Y axis when I shift gravity from the Y to Z axis?
Hello everyone. I'm working on a script that will allow the player to stick to a patch on the wall by changing the gravity for the patch. However, once the player is on the patch, my Vertical movement stops working properly. Horizontal still works fine, but I can't move up the wall and when I try to move down the wall, after a small amount of input time, I fall off to the floor.
I'm assuming this has something to do with having changed the orientation of gravity, but if so I don't know why. Can anybody lend me a hand?
private Vector3 newGravity = new Vector3(0f, 0f, 9.81f);
private Vector3 oldGravity = new Vector3(0f, -9.81f, 0f);
public float speedAdjust;
private bool canJump = false;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Physics.gravity = newGravity;
//other.rigidbody.useGravity = false;
other.rigidbody.velocity = Vector3.zero;
PlayerController.movement = new Vector3(PlayerController.movementHorizontal, (PlayerController.movementVertical * speedAdjust), 0f);
canJump = true;
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
Physics.gravity = oldGravity;
//other.rigidbody.useGravity = true;
PlayerController.movement = new Vector3(PlayerController.movementHorizontal, 0f, PlayerController.movementVertical);
canJump = false;
}
}
Answer by coptout · Nov 07, 2013 at 06:14 PM
I figured it out. Shame on me, since this is the second time I've made a similar mistake. Line 13 is only being called once, when you enter the trigger but in the PlayerController script, the original variable is set during Fixed Update(), so it was immediately resetting after entering the trigger area. Hooray for figuring it out on my own!
Your answer
![](https://koobas.hobune.stream/wayback/20220613121415im_/https://answers.unity.com/themes/thub/images/avi.jpg)