【问题标题】:How to use a bool to determine whether the player is jumping over如何使用 bool 来判断玩家是否跳过
【发布时间】:2021-03-29 18:45:39
【问题描述】:

我正在为我正在开发的游戏制作一些动画(仍处于早期阶段,并且是编码新手)。现在我正在尝试使用 bool 来检测玩家是否在跳跃,然后如果它返回 true,那么动画就会播放。

角色控制器:

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

public class Character2dController : MonoBehaviour
{
    public float MovementSpeed = 8;
    public float JumpForce = 5;
    public Transform feet;

    public bool isJumping;
  
    private Rigidbody2D rb;

    public LayerMask groundLayers;

   
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        isJumping = false;

    }

    // Update is called once per frame
    void Update()
    {
        Jump();
   
        //LR movement
        var movement = Input.GetAxis("Horizontal");
        transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
        
        isJumping = false;

    }

    //Jumping
    void Jump()
    {
        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            isJumping = true;
            rb.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);

        }
    }

    //Grounding
    public bool IsGrounded()
    {
        Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
        
        if (groundCheck.gameObject != null)
        {
            return true; 
        }
        return false;
    }

}

角色动画:

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

public class CharacterAnimation : MonoBehaviour
{

    private Animator anim;
    private bool isJumping;

    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        isJumping = GetComponent<Character2dController>().isJumping;
        isJumping = false;
        
    }

    // Update is called once per frame
    void Update()
    {
        //Walking Anim
        if (Input.GetKey("a") || Input.GetKey("d"))
        {
            anim.SetBool("isWalking", true);
        }
        else
        {
            anim.SetBool("isWalking", false);
        }

        //Character flipping
        if (Input.GetKey("a"))
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        if (Input.GetKey("d"))
        {
            transform.localScale = new Vector3(1, 1, 1);
        }

        if(isJumping == true)
        {
            anim.SetTrigger("Up");
        }

    }

}

然而,isJumping bool 似乎永远不会变为 true,因此动画永远不会播放。我已经尝试将它放在字符控制器代码中的多个位置,但它们似乎都不起作用。我想知道我是否做错了什么,或者我是否应该使用不同的方式来为玩家跳跃设置动画。 同样,我对编码还很陌生,所以如果有任何问题,请随时告诉我。谢谢!

【问题讨论】:

  • 作为一种方法,这是使用状态机模式的理想选择 - 您可以在此处阅读:gameprogrammingpatterns.com/state.html 按照您的方式进行操作的问题是它很快就会变得管理起来太复杂 - 一旦你开始尝试检测你是从陆地还是水中,从站立、行走、跑步等中跳起还是二段跳。

标签: c# unity3d animation


【解决方案1】:
public class CharacterAnimation : MonoBehaviour
{

    private Animator anim;
    private bool isJumping;

    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        isJumping = GetComponent<Character2dController>().isJumping;//assigns value
        isJumping = false;
        
    }
.
.
.

我注释的行仅将Character2dController 类中isJumping 变量的值分配给CharacterAnimation 类中的isJumping 变量。因此,当您更改Character2dController 类中的isJumping 变量时,CharacterAnimation 类中的isJumping 变量不会更新。

因此,您可以检查Character2dController 类中的isJumping 变量:

public class CharacterAnimation : MonoBehaviour
{

    private Animator anim;
    private Character2dController character2dController;

    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        character2dController = GetComponent<Character2dController>();//now we are holding a reference to the Character2dController component
    }
    // your other code here
    if(character2dController.isJumping == true)
    {
        anim.SetTrigger("Up");
    }

另外,在Character2dController 中,您在每一帧都将isJumping 设置为false,我相信这不是您想要做的。与其直接设置为false,不如检查自己是否接地,并相应更新isJumping

【讨论】:

  • 好的,谢谢。调用对整个 Character2dController 的引用比仅引用 isJumping 更有意义。一个问题。如果我不想在 Update() 函数中不断地将 isJumping 设置为 false ,我应该在哪里将 isJumping 更新为 false ,我应该怎么做?现在我在Jump() 中将isJumping 设置为true,并有一个if 语句检查IsGrounded() 是否为真,并以这种方式将isJumping 更新为false。但它似乎并没有真正起作用。我只是收到很多错误消息
  • 你正在检查你的角色是在地上还是在IsGrounded()函数中跳跃。当您在地面上时,将isJumping 设置为false,然后返回true。您可以在Update() 中拨打IsGrounded()
  • 现在看来可以了。我将不得不继续稍微调整它,但我现在对此感到满意。感谢您的帮助!
【解决方案2】:

尝试使用Keycode. 获取输入,如下所示:

if (Input.GetKey(KeyCode.A)) {
    Debug.LogError("A");
}

您可以尝试Debug.LogError("Message on key pressed") 以确保您正在捕获输入。

如果您确定自己正确捕获了输入,则可以在问题中指出这一点,以便可能的帮助者知道深入研究代码。 同时发布您可能遇到的任何控制台错误。

还要确保设置了控制动画的参数,例如在anim.SetBool("isWalking", true); 中,如果设置了动画发生的参数,您可以在编辑器本身中进行检查。

【讨论】:

    猜你喜欢
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    相关资源
    最近更新 更多