Как сделать управление для 2D персонажа в Unity на ПК, без двойного прыжка и с поворотом игрока в разные стороны

Как сделать управление для 2D персонажа в Unity на ПК, без двойного прыжка и с поворотом игрока в разные стороны


Ответы (1 шт):

Автор решения: affenate

Как настроить скрипт показано в видео

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class PF : MonoBehaviour
{
    Rigidbody2D rb;
    public float speed;
    public float jumpheight;
    public Transform groundCheck;
    bool isGrounded;
    Animator anim;

    void Start()    
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        Flip();
        CheckGround();
        rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
            rb.AddForce(transform.up * jumpheight, ForceMode2D.Impulse);

    }
    void Flip()
    {
        if (Input.GetAxis("Horizontal") > 0)
        transform.localRotation = Quaternion.Euler(0, 0, 0);
        if (Input.GetAxis("Horizontal") < 0)
        transform.localRotation = Quaternion.Euler(0, 180, 0);
    }
    void CheckGround()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 1);
        isGrounded = colliders.Length > 1;
    }
}
→ Ссылка