- Home /
[fixed] Player movement after changing gravity direction
Context: My goal is to make a first person game where the player can change the direction of gravity in the 6 main directions. Everything with a rigidbody, including the player, is affected by the change of gravity. The player is rotated so they are still standing "up" based on the new gravity direction, and the player can move around like normal (basically, the wall becomes the new floor).
What works: The gravity direction can be changed by the player, and they rotate for the new direction, and mouse look works great.
What doesn't: Player movement when gravity is different. Normally, the direction that you move in is based on the vertical and horizontal input axis, and then adjusted based on the y axis rotation (meaning pressing 'w' makes you walk in the direction in which you are looking). This works correctly for regular gravity, but how do you calculate the right direction to move in when everything is turned sideways or upside down?
How I have it set up: The player is a sphere, and the camera is a child of the sphere. The sphere has a rigidbody, and a script called "Mover.cs" that gets input and adds forces to the sphere to move it. Mover has a reference to the transform of the camera so that moving forward means moving in the direction the camera is facing. It also has a script called "GravityController.cs" that gets input from certain keys and changes the value of physics.gravity. When changing the gravity, it makes the sphere kinematic, rotates it to the new "up", and then makes it not kinematic so physics continue to affect it like normal. The camera, which is a child of the sphere, has "MouseLook.cs" on it which gets the x and y movements of the mouse - this is just the script provided by unity.
Edit: I figured it out, all is working right now. I've changed the code below to reflect that.
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public Transform cam;
public float power = 20f;
void FixedUpdate () {
Vector3 directionVector = Vector3.zero;
Vector3 grav = Physics.gravity.normalized;
Quaternion rotation = Quaternion.identity;
float horiz = Input.GetAxis ("Horizontal");
float vert = Input.GetAxis ("Vertical");
int g = 0;
if(grav == Vector3.down){
directionVector = new Vector3 (horiz, 0f, vert);
rotation.eulerAngles = new Vector3 (0f, cam.eulerAngles.y, 0f);
g = 0;
}
else if(grav == Vector3.left){
directionVector = new Vector3 (horiz, 0f, vert);
rotation.eulerAngles = cam.eulerAngles;
g = 1;
}
// ... continue for other gravity cases
// Basically code from the FPSInputController
if (directionVector != Vector3.zero) {
float directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
directionLength = Mathf.Min(1, directionLength); // 1 or less
//directionLength = directionLength * directionLength; // square
directionVector = directionVector * directionLength;
}
Vector3 usefulVector = rotation * directionVector;
switch(g){
case 1: usefulVector.x = 0f;
break;
// ... continue for other gravity cases
}
rigidbody.AddForce (usefulVector * power);
}
}
The place where I've marked <<<<< in the code above is where I'm just unsure about. Let me know if there is other information I can provide. Thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220613143437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to change the angle my character falls at 1 Answer
Why is Quaternion.FromToRotation() not working properly? 3 Answers
CharacterController.Move uses gravity even though it shouldn't 1 Answer
click to move workig script!! but pls help with rotation!! :( 1 Answer
Character too floaty after jumping and grappling.,Grapple too floaty 0 Answers