- Home /
Why Does My Player Rotate to 180 to 0 Constanly
I am working on a platformer and am making a gravity based level where gravity Switches randomly as you go though the level, I was almost Done with it but for some reason when i try it out every time the player goes though the trigger the rotation of the players keeps going 180 to 0 every frame here a look at the script //Boolean variables can be true or false you'd use this to see wheather something is happening or a variable is set.
public Animator animator;//Animates our character.
private bool m_FacingRight = true; // For determining which way the player is currently facing.
private bool m_FacingGravity = true;
public float distToGround = 0.5f;
//RigidBody is basically a physical object in unitys engine lets us know it takes up space, and allows it to use gravity
public Rigidbody rb;//Container variable for my players Rigidbody
//float is basically a number but it can be negative , positive or a decimal number.
public float sidSpd;//how fast is my player moving left or right (Left this will be negative, RIght this willbe positive)
public static float moveSpd = 10;//A variable were going to ue later on in the tutorial to increase players speed or decrease it
public float jumpForce = 10f;//how much force is the player using to jump. gravity is always pushing down on player so he needs X amount of force to jump
public float seemoveSpd;
public GameObject Laser;// Lets the Player Attack
public bool IsUnable = false;
public static int Unlock = 0;
public bool Gravityss = GravitySwitch.IsGravity;
//Built in unity function Use this for initialization any variable that cannot be empty. Like rigidbody for example. this runs once the game loads or the stage is loaded
void Start ()
{
//set the RigidBOdy of the player inside my RB variable.
rb = GetComponent<Rigidbody> ();///GetComponent<> Basically grabs any component attached to the current object. (object this script is on)
}
//Built in unity function Update is called once per frame
//most math based equations go here. You should not be moving your character or any object in update when working with Physics or rigidbodies
void Update ()
{// Pay attention for this Part
Gravityss = GravitySwitch.IsGravity;
if(Gravityss == false) {
transform.Rotate(180,0,0);
}
else
{
transform.Rotate(0, 0, 0);
}// Part to pay attention ends
seemoveSpd = moveSpd;
if (sidSpd > 0 && !m_FacingRight)
{
// ... flip the player.
Flip();
}
// Otherwise if the input is moving the player left and the player is facing right...
else if (sidSpd < 0 && m_FacingRight)
{
// ... flip the player.
Flip();
}
sidSpd = Input.GetAxis("Horizontal") * 10;
if (Input.GetButtonDown("Cancel")){
SceneManager.LoadScene("LevelSelect");
}
animator.SetFloat("Speedtomove", Mathf.Abs(sidSpd));
if (Input.GetButtonDown("Fire2"))
{
Laser.SetActive(true);
}
if (Input.GetButtonUp("Fire2"))
{
Laser.SetActive(false);
}
}
//Built in unity function runs on a fixed time scale cam be changed in setting. All physics based movements should go in here
void FixedUpdate()
{
rb.AddForce (new Vector3(sidSpd,0,0));//Add a force to my players RigidBody.
//The force must be a vector 3 for this script so it adds force in 3 Dimensions.
if (Input.GetKey(KeyCode.Space) && isGrounded())
{
//rb.AddForce(0, jumpForce, 0); dang it!
Vector3 jumpVelocity = new Vector3(0f, jumpForce, 0f);
rb.velocity = rb.velocity + jumpVelocity;
}
if (Input.GetKeyDown(KeyCode.S) && (Input.GetKey(KeyCode.LeftShift) && (Unlock > 0)))
{
Vector3 StompVelocity = new Vector3(00f,-20f, 0f);
rb.velocity = rb.velocity + StompVelocity;
}
if (Input.GetKey(KeyCode.Space) && IsTop() && GravitySwitch.IsGravity==(false))
{
//rb.AddForce(0, jumpForce, 0); dang it!
Vector3 jumpVelocity = new Vector3(0f, -jumpForce, 0f);
rb.velocity = rb.velocity + jumpVelocity;
}
}
//Built in unity function to detect When a collision has occured
//Built in unity function to detect When a collision has ended
bool isGrounded()
{
return Physics.Raycast (transform.position, Vector3.down, distToGround);
}
bool IsTop()
{
return Physics.Raycast(transform.position, Vector3.up, distToGround);
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
public void LoadPlayer()
{
SaveSystem data = SaveSystemController.saveSystem();
Unlock = data.Meee;
}
public void SavePlayer()
{
SaveSystemController.SavePlayerUnlocks(this);
}
}
and the other Script
public Vector3 Physicss;
public PlatformControls controls;
public static bool IsGravity = true;
public bool evidence = IsGravity;
// Start is called before the first frame update
void Start()
{
Physicss = Physics.gravity;
}
// Update is called once per frame
void Update()
{
Physics.gravity = Physicss;
evidence = IsGravity;
}
void OnTriggerEnter(Collider other)
{
if((other.tag == "Player")&& (IsGravity = (true)))
{
Physicss.y = 9.81f;
IsGravity = false;
}
}
I Don't really know how to fix this, Please help
Answer by mchts · Apr 19, 2019 at 07:00 AM
if(Gravityss == false) {
transform.Rotate(180,0,0);
}
else
{
transform.Rotate(0, 0, 0); //this line will do nothing and so it's redundant
// Part to pay attention ends
If your Gravityss
is false your object will rotate 180 degrees each frame. So you will see 180 to 0 and to 180 again so on... continiously. If you want to rotate to a certain degree you should just set rotation to a constant value like transform.eularAngles = new Vector3(180, 0, 0)
:
Your answer
![](https://koobas.hobune.stream/wayback/20220612195740im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get Ray rotation from direction 0 Answers
How to change the angle my character falls at 1 Answer
Rotating objects to specific angle 3 Answers
make projectiles always hit target 1 Answer