Я пытаюсь написать программу для моего персонажа, чтобы прыгать. Это идет с ошибками. Я не совсем уверен, как их исправить - PullRequest
0 голосов
/ 15 марта 2019

Я пытаюсь сделать свою игру.У меня есть персонаж, и я пытаюсь заставить его прыгать.Я могу заставить персонажа ходить, бездействовать и скользить, но он не будет прыгать.Я делаю все кодирование в 1 сценарий.Я получаю прикрепленные сообщения об ошибках, но не знаю, как их исправить.Я пробовал несколько вещей, но не могу заставить их работать.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

public class Player : MonoBehaviour
{
    private Rigidbody2D myRigidbody;

    private Animator myAnimator;

    [SerializeField]
    private float movementSpeed;

    private bool walk;

    private bool slide;

    private bool facingRight;

    [SerializeField]
    private Transform[] groundPoints;

    [SerializeField]
    private float groundRadius;

    private LayerMask whatIsGround;

    private bool IsGrounded;

    private bool jump;

    [SerializeField]
    private bool airControl;

    [SerializeField]
    private float jumpForce;


    // Start is called before the first frame update
    void Start()
    {

        facingRight = true;
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();

    }

    void Update()
    {

        HandleInput();
    }


    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");

        isGround = IsGrounded();

        HandleMovement(horizontal);

        Flip(horizontal);

        HandleWalk();

        ResetValues();

    }

    private void HandleMovement(float horizontal)
    {
        if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Walk")&& (isGround || airControl))
        {

            myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);
        }
        if (isGround && jump)
        {
            isGround = false;
            myRigidbody.AddForce(new Vector2(0, jumpForce));
        }
        if (slide && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
        {
            myAnimator.SetBool("slide", true);
        }
        else if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("slide"))
        {
            myAnimator.SetBool("slide", false);
        }

        myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
    }

    private void HandleWalk()
    {
        if (walk)
        {
            myAnimator.SetTrigger("walk");
            myRigidbody.velocity = Vector2.zero;
        }

    }

    private void HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jump = true;
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            walk = true;
        }
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            slide = true;
        }
    }


    private void Flip(float horizontal)
    {
        if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            facingRight = !facingRight;

            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;
        }
    }

    private void ResetValues()
    {
        walk = false;
        slide = false;
    }

    private bool isGround()
    {

        if (myRigidbody.velocity.y <= 0)
        {
            foreach (Transform point in instance.groundPoints)
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll (point.position, instance.groundRadius, instance.whatIsGround);

                for (int i = 0; i < colliders.Length; i++)
                {
                    if (colliders[i].gameObject != gameObject)
                    {
                        return true;
                    }
                }
            }

        }
        return false;
    }
}

Сообщения об ошибках для игрока, чтобы прыгнуть

1 Ответ

0 голосов
/ 15 марта 2019

Переименовать поле private bool IsGrounded; в private bool isGround;

Также необходимо изменить имя метода private bool isGround() {} на private bool IsGrounded() {}

И удалить instance из IsGrounded метода.

private bool IsGrounded()
    {
        if (myRigidbody.velocity.y <= 0)
        {
            foreach (Transform point in groundPoints)
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatisGround);
                for (int i = 0; i < colliders.Length; i++)
                {
                    if (colliders[i].gameObject != gameObject)
                    {
                        return true;
                    }
                }
            }
        }
            return false;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...