I need some help as I have tried multiple different script ideas and types and nothing seems t be working correctly. The player moves with A and D, jumps with space. I want to make it so if the player presses the Q or E key, the player rotates 90 degrees to the right or left. The camera would also rotate with them etc.
What I want to happen is the gravity also rotates. So if the player is rotated 90 degrees to the right, the gravity also rotates 90 degrees. So now the player is falling to the right (or left, trying to picture it) and all movement controls act the same, its just that everything has rotated.
I would provide a script, but I have tried so many at this point and none have worked.
I can provide the movement script:
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 15f;
public Rigidbody playerBody;
public int forceConst = 50;
private bool onGround;
private Vector3 direction;
// Update is called once per frame
void Update()
{
direction = new Vector3(Input.GetAxisRaw("Horizontal"),0 , 0).normalized; // gives control on x axis
if (Input.GetKeyDown(KeyCode.Space) && onGround == true)
{
playerBody.AddForce(transform.up * forceConst, ForceMode.Impulse);
onGround = false;
}
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Wall")
{
onGround = true;
}
}
private void FixedUpdate()
{
playerBody.MovePosition(playerBody.position + transform.TransformDirection(direction) * moveSpeed * Time.deltaTime); // moves player in direction
}
}
The gravity is nothing special and is using the normal Unity gravity.
GravityRotationChecker gRC;
private void Start()
{
gRC = GetComponent<GravityRotationChecker>();
Physics2D.gravity = new Vector2(0, -9.81f);
//on start set gravity to normal (down)
}
private void FixedUpdate()
{
if (gRC.JumpDirectionToTheLeft)
{//if GravityIsRotating is true, rotate the player's gravity to the right
RotatePlayersGravity();
}
if (gRC.JumpDirectionUpsideDown)
{//if JumpDirectionUpsideDown is true, rotate the player's gravity Up
RotatePlayerGravityUpsideDown();
}
}
//function change gravity to the right
void RotatePlayersGravity()
{
Physics2D.gravity = new Vector2(9.81f, 0);
gRC.GravityIsRotating = false;
}
//function to flip the gravity Upside down
void RotatePlayerGravityUpsideDown()
{
Physics2D.gravity = new Vector2(0, 9.81f);
gRC.GravityIsRotating = false;
}
//function to change gravity to the left (currently not using).
void RotatePlayerGravityToLeft()
{
Physics2D.gravity = new Vector2(-9.81f, 0);
gRC.GravityIsRotating = false;
}
The GravityRotationChecker just sets the appropriate bool when the trigger is hit. In your case, you would change a bool when you press the button to rotate not only the player, but also the gravity.
Thank You! Just curious since I am still a little bit confused, what would the gRC script look like? is it just some bools? sorry I am quite new to unity.
All good. The gRC or GravityRotationChecker class sits on my player gameobject just like the RotatePlayer script i shared. I declare the bools you see above and then when I hit a certain trigger i set it to true and the others to false. When i hit the next trigger i set the appropriate bool to true and the others to false. So yours would have probably be set by Input.GetButton(button name).
You could design a class that dictates the gravity based upon the direction,velocity, rate, speed, etc… Reference the script and pass your parameters into that constructor. Way easier than what you two are trying to do.
It’s worth a try. You just need to collect your player’s/enemies current stats and store them into a variable then pass it into the constructor GravityHandler.handleGravity(direction, speed);
Something like that shouldn’t be terrible complicated. Of course you’d probably require more information but generally the same thing.