- Home /
Gravity and rotation control
I am making a game where you can change your gravity to 6 diffirent directions. I am using the standard rigidbody fps character controller as the player. I have 2 problems however. 1: the change in gravity affects the whole world and not just the player, i only want it to affect the player. 2: I have code to rotate the player but however it wont rotate the controller, i have tried to do it in the inspector during play mode but that didnt work either. Here is the code (I havent finished filling in all the rotations):`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gravity : MonoBehaviour {
public float GravityDirection = 1;
// Gravity direction 1 is the normal dirrection of gravity
void Start () {
}
void FixedUpdate ()
{
#region Find if keys down
if (Input.GetKeyDown(KeyCode.Alpha1) && GravityDirection != 1)
{
GravityDirection = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && GravityDirection != 2)
{
GravityDirection = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && GravityDirection != 3)
{
GravityDirection = 3;
}
if (Input.GetKeyDown(KeyCode.Alpha4) && GravityDirection != 4)
{
GravityDirection = 4;
}
if (Input.GetKeyDown(KeyCode.Q) && GravityDirection != 5)
{
GravityDirection = 5;
}
if (Input.GetKeyDown(KeyCode.E) && GravityDirection != 6)
{
GravityDirection = 6;
}
#endregion
#region Change gravity
if (GravityDirection == 1)
{
Physics.gravity = new Vector3(0, -9.81f, 0);
if (GravityDirection == 2)
{
transform.Rotate(180, 0, 0);
}
if (GravityDirection == 3)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 4)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 5)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 6)
{
transform.Rotate(0, 0, 0);
}
}
if (GravityDirection == 2)
{
Physics.gravity = new Vector3(0, 9.81f, 0);
if (GravityDirection == 1)
{
transform.Rotate(180, 0, 0);
}
if (GravityDirection == 3)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 4)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 5)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 6)
{
transform.Rotate(0, 0, 0);
}
}
if (GravityDirection == 3)
{
Physics.gravity = new Vector3(0, 0, -9.81f);
if (GravityDirection == 2)
{
transform.Rotate(-90, 0, 0);
}
if (GravityDirection == 1)
{
transform.Rotate(90, 0, 0);
}
if (GravityDirection == 4)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 5)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 6)
{
transform.Rotate(0, 0, 0);
}
}
if (GravityDirection == 4)
{
Physics.gravity = new Vector3(0, 0, 9.81f);
if (GravityDirection == 2)
{
transform.Rotate(90, 0, 0);
}
if (GravityDirection == 3)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 1)
{
transform.Rotate(-90, 0, 0);
}
if (GravityDirection == 5)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 6)
{
transform.Rotate(0, 0, 0);
}
}
if (GravityDirection == 5)
{
Physics.gravity = new Vector3(-9.81f, 0, 0);
if (GravityDirection == 2)
{
transform.Rotate(0, 0, 90);
}
if (GravityDirection == 3)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 4)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 1)
{
transform.Rotate(0, 0, -90);
}
if (GravityDirection == 6)
{
transform.Rotate(0, 0, 0);
}
}
if (GravityDirection == 6)
{
Physics.gravity = new Vector3(9.81f, 0, 0);
if (GravityDirection == 2)
{
transform.Rotate(0, 0, -90);
}
if (GravityDirection == 3)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 4)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 5)
{
transform.Rotate(0, 0, 0);
}
if (GravityDirection == 1)
{
transform.Rotate(0, 0, 90);
}
}
#endregion
}
} `
Try use a "Constant Force" component to a add counter force to nullify the gravity plus the the force in the direction you want.