L
L
L1meDelayProgram2021-01-16 14:45:18
Unity
L1meDelayProgram, 2021-01-16 14:45:18

How to make the character turn in the direction of movement in Unity (the rotation code is already there but does not work correctly)?

please tell me how to make it so that when walking the character would turn in place and not teleport (that is, I press the walk button to the left, and the character at that time is turned to the right and he teleports some distance to the right (and vice versa) and only then turns to the left) please answer. ALL good.
HERE is the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerControl : MonoBehaviour
{
    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;

    private bool facingRight = true;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate()
    {
        moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
        if(facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if(facingRight == true && moveInput < 0)
        {
            Flip();
        }
    }
    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question