D
D
Danila Danilov2020-12-28 14:09:28
C++ / C#
Danila Danilov, 2020-12-28 14:09:28

The character moves, but does not turn in the direction of movement. How to fix it?

The character moves and there is an animation of movement, jumping and standing still, but he does not turn in the direction of movement and looks at one point. Please help me fix this.
Here is the code:

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

public class Player : MonoBehaviour
{
    public float speed;
    public float jumpForce;
    private float moveInpute;

    private Rigidbody2D rb;

    private bool facingRight = true;

    private bool isGrounded;
    public Transform feetpos;
    public float checkRadius;
    public LayerMask whatIsGround;

    private Animator anim;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }
    private void FixedUpdate()
    {
        moveInpute = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInpute * speed, rb.velocity.y);
        if (facingRight == false && moveInpute > 0)
        {
            Flip();
        }
        else if (facingRight == true && moveInpute < 0)
        {
            Flip();
        }
        if (moveInpute == 0)
        {
            anim.SetBool("isRunning", false);
        }
        else
        {
            anim.SetBool("isRunning", true);
        }
    }
    private void Update()
    {
        isGrounded = Physics2D.OverlapCircle(feetpos.position, checkRadius, whatIsGround);

        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = Vector2.up * jumpForce;
            anim.SetTrigger("takeOF");
        }
        if (isGrounded == true)
        {
            anim.SetBool("isJumping", false);
        }
        else
        {
            anim.SetBool("isJumping", true);
        }
    }
    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

1 answer(s)
G
GFX Data, 2020-12-28
@ShockWave2048

transform.rotation = Quaternion.LookRotation(rb.velocity);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question