Answer the question
In order to leave comments, you need to log in
Why is my character not turning?
I faced such a problem that my character walks but does not turn when necessary. Here is the script
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private float horizontalSpeed;
float speedX;
public float verticalImpulse;
Rigidbody2D rb;
bool isGrounded;
private bool facingRight = true;
void Start() {
rb = GetComponent<Rigidbody2D>();
}
public void LeftButtonDown()
{
speedX = -horizontalSpeed;
}
public void RightButtonDown()
{
speedX = horizontalSpeed;
}
public void Stop()
{
speedX = 0;
}
public void OnClickJump()
{
if(isGrounded)
rb.AddForce(new Vector2(0, verticalImpulse), ForceMode2D.Impulse);
}
void FixedUpdate () {
transform.Translate(speedX, 0, 0);
if (facingRight == false && horizontalSpeed > 0)
{
Flip();
}
else if (facingRight == true && horizontalSpeed < 0)
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = false;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question