c# - 如何使用 bool 来判断玩家是否跳过
问题描述
我正在为我正在开发的游戏制作一些动画(仍处于早期阶段,并且是编码新手)。现在我正在尝试使用 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,因此动画永远不会播放。我已经尝试将它放在字符控制器代码中的多个位置,但它们似乎都不起作用。我想知道我是否做错了什么,或者我是否应该使用不同的方式来为玩家跳跃设置动画。同样,我对编码还很陌生,所以如果有任何问题,请随时告诉我。谢谢!
解决方案
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;
}
.
.
.
我注释的行仅将类中变量的值分配给您isJumping
的类中的变量。因此,当您更改类中的变量时,类中的变量不会更新。Character2dController
isJumping
CharacterAnimation
isJumping
Character2dController
isJumping
CharacterAnimation
因此,您可以检查类中的isJumping
变量Character2dController
:
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
相应地更新。
推荐阅读
- oop - 我可以向用户类添加函数 addtocart()、removefromcart()、createorder()、executeorder() 吗?
- ruby-on-rails - 为什么我为 Rails 运行 db:migrate 后 schema.rb 会更新?
- javascript - 仅选择选项-单选按钮-动态表单-Yii2
- java - Swing - 尝试在除以零时使简单的计算器显示错误
- xaml - Xamarin XAML 绑定路径与孩子
- c++ - 我在哪里放置 C++ 自定义异常代码?
- ruby-on-rails - docker-compose 连接到 redis + sidekiq 时出错
- react-native - 如何在 Google Oauth2 上配置自定义 url
- javascript - 遍历 JSON 以仅更新单个元素
- cordova - 了解如何使用 cordova-plugin-dialogs 文档示例 8.0.0