[英]I'm try to make game using unity and my Jump functionality not working
I'm trying to make simple 2D game following tutorials when I did same thing as tutorial my jump functionality not working and left and right move functionality working please help me below I attached my source code and relevant screen shot我正在尝试按照教程制作简单的 2D 游戏,当我做与教程相同的事情时,我的跳跃功能不起作用,左右移动功能起作用,请在下面帮助我,我附上了我的源代码和相关屏幕截图
my player class我的播放器类
public class Player : MonoBehaviour
{
private Rigidbody2D _rigid;
//variable for jump
[SerializeField]
private float _jumpForce = 5.0f;
[SerializeField]
private LayerMask _grondLayer;
private bool _resetJump = false;
// Start is called before the first frame update
void Start()
{
_rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
float move = Input.GetAxisRaw("Horizontal");
_rigid.velocity = new Vector2(move,_rigid.velocity.y);
if(Input.GetKeyDown(KeyCode.Space) && IsGrounded()==true)
{
Debug.Log("jump");
_rigid.velocity = new Vector2(_rigid.velocity.x,_jumpForce);
StartCoroutine(ResetJumpNeededRoutine());
}
}
bool IsGrounded()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, _grondLayer);
if(hitInfo.collider != null)
{
if(_resetJump==false){return true;}
}
return false;
}
IEnumerator ResetJumpNeededRoutine()
{
_resetJump = true;
yield return new WaitForSeconds(0.1f);
_resetJump = false;
}
}
在二维字符上实现跳转机制的正确方法。
_rigid.AddForce(new Vector2(0, _jumpForce), ForceMode2D.Impulse);
The problem is probably the LayerMask you selected Ground
layer to be ignored therefore IsGrounded
function will return false.问题可能是您选择的
Ground
层的 LayerMask 被忽略,因此IsGrounded
函数将返回 false。
What you wanna do is select the layers you'd like your Raycast to ignore (All except Ground
I assume) in the unity editor then give it another go.您想要做的是在统一编辑器中选择您希望 Raycast 忽略的图层(我假设除
Ground
之外的所有图层),然后再试一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.