Персонаж двигается и анимация движения, прыжков и стояния на месте есть, но он не поворачивается в сторону движения и смотрит в одну точку. Помогите пожалуйста это исправить.
Вот код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroControl : MonoBehaviour
{
[SerializeField] private float speed = 3f;
[SerializeField] private int lives = 5;
[SerializeField] private float jumpforce = 15f;
private bool isGrounded = false;
private Rigidbody2D rb;
private Animator anim;
private SpriteRenderer sprite;
private States State
{
get { return (States)anim.GetInteger("state"); }
set { anim.SetInteger("state" , (int)value);}
}
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
private void FixedUpdate()
{
CheckGround();
}
private void Update()
{
if (isGrounded) State = States.idle;
if (Input.GetButton("Horizontal"))
Run();
if (isGrounded && Input.GetButtonDown("Jump"))
Jump();
}
private void Run()
{
if (isGrounded) State = States.run;
Vector3 dir = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + dir , speed * Time.deltaTime);
sprite.flipX = dir.x < 0.0f;
}
private void Jump()
{
rb.AddForce(transform.up * jumpforce , ForceMode2D.Impulse);
}
private void CheckGround()
{
Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 0.3f);
isGrounded = collider.Length > 1;
if (!isGrounded) State = States.jump;
}
}
public enum States
{
idle,
run,
jump
}