我正在尝试为我的角色编写一个程序来跳转。它出现了错误。我不太确定如何修复它们

问题描述 投票:-1回答:1

我正在尝试制作我的游戏。我有一个角色,我想让它跳起来。我可以让角色走路,闲置和滑动,但它不会跳跃。我正在用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;
    }
}

Error messages for player to jump

c# unity3d
1个回答
0
投票

将字段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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.