How to Rotate the player to move in the facing direction in 2D?
Hey guys I know this question is asked a lot here, but I can't seem to make it work. I am working on a left to right 2D RPG game and when my player "walks" facing down a slope, he falls over. I don't want to freeze the Z axis because I want his feet to follow incline and decline slopes. Here is my code, but he just falls over when the Z axis isn't frozen. Any help would be highly appreciated!!
void FixedUpdate()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position -= transform.right * Time.deltaTime * maxSpeed;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += transform.right * Time.deltaTime * maxSpeed;
}
float move = Input.GetAxis("Horizontal");
if (move > 0 && !facingRight)
{
Flip();
}
else if (move < 0 && facingRight)
{
Flip();
}
anim.SetFloat("move", Mathf.Abs(move));
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
first off, change the rigidbody velocity of the guy ins$$anonymous$$d of setting his position, you're teleporting him, which bypasses physics. and we'll see after that.
The rigid body physics are making him fall over/rotate. How would I put it in code to make the rigidbody velocity not rotate him as the guy goes down the slope?
Your answer
Follow this Question
Related Questions
how to convert a Vector3 to a float value? 1 Answer
Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers
How to limit the rotation of transform.Rotate Z in 2D 0 Answers
Rotate around a point with a specific angle 0 Answers
How to make camera moving as the player on a planet 0 Answers