I
I
ILIIA22892019-11-11 08:22:51
Unity
ILIIA2289, 2019-11-11 08:22:51

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

1 answer(s)
N
namee, 2019-11-15
@namee

does it accidentally have the static property turned on?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question