首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用bool来确定玩家是否跳过

如何使用bool来确定玩家是否跳过
EN

Stack Overflow用户
提问于 2020-12-19 17:41:13
回答 2查看 51关注 0票数 0

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

字符控制器:

代码语言:javascript
运行
复制
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;
    }

}

CharacterAnimation:

代码语言:javascript
运行
复制
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布尔值似乎永远不会更改为true,因此动画永远不会播放。我试着把它放在字符控制器代码中的不同位置,但似乎都不起作用。我想知道我是不是做错了什么,或者我是否应该用一种不同的方式来使玩家跳跃成为动画。再说一次,我是编程新手,所以如果有什么不对劲的地方,请随时让我知道。谢谢!

EN

回答 2

Stack Overflow用户

发布于 2020-12-19 18:17:42

代码语言:javascript
运行
复制
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变量:

代码语言:javascript
运行
复制
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,我相信这不是您想要做的。您应该检查是否接地,并相应地更新isJumping,而不是直接将其设置为false

票数 1
EN

Stack Overflow用户

发布于 2020-12-19 18:14:02

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

代码语言:javascript
运行
复制
if (Input.GetKey(KeyCode.A)) {
    Debug.LogError("A");
}

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65368457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档