- Home /
Rotation not working properly(3D)
Basically I was trying to rotate the player 180 degrees around the z-axis to correspond with the gravity change that happens when you hold down two buttons. The result I got from this script is that the player will rotate, but then the gravity wouldn't shift, and when I'd try to shift gravity again, it would shift, but then the rotation would be off. Here is the script that I used:
//Movement
float moveSpeed = 3000f;
float x, z;
float jumpHeight = 5f;
//Gravity
public float gravityForce = 10f;
float gravMultiplier = 1f;
//Random Stuff
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
if(Input.GetKey(KeyCode.Mouse2))
{
ShiftGravity();
}
}
void FixedUpdate()
{
//Movement
rb.AddForce(Vector3.forward * z * moveSpeed * Time.deltaTime);
rb.AddForce(Vector3.right * x * moveSpeed * Time.deltaTime);
}
private void ShiftGravity()
{
if (Input.GetKeyDown(KeyCode.W))
{
gravMultiplier *= -1;
Physics.gravity = new Vector3(0, gravityForce * gravMultiplier);
RotationFromGravity();
}
}
private void RotationFromGravity()
{
transform.eulerAngles = new Vector3(0, 0, 180);
}
}
Answer by Vipertex13 · Apr 08, 2020 at 05:21 AM
Hello!
This is an easy way to find the rotation of an object in the X-axis. it is a very simple and easy code.
float x;
void Update ()
{
x += Time.deltaTime * 10;
transform.rotation = Quaternion.Euler(x,0,0);
}
Your answer
Follow this Question
Related Questions
How to rotate camera around 3D object without breaking camera rotation 0 Answers
Grappling hook springjoint 0 Answers
How can I rotate my player with a Joystick? 0 Answers
Artificial Gravity "SpaceShip" 1 Answer
Detect if on ground? 1 Answer