[英]Double Jump in unity 2D and code optimization
我是 Unity 和 C# 的绝对初学者,在我的 Unity 2D 项目中插入双跳时遇到了一些问题。 我的问题是:我怎样才能在这段代码上添加双跳? 我尝试在互联网上遵循很多教程,但我在任何教程中都没有成功。 我现在使用的这段代码运行正常,但是我需要在游戏中添加双跳功能。 这是我正在使用的代码,我真的需要一些帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rigidbody;
private BoxCollider2D collider;
private SpriteRenderer sprite;
private Animator animator;
[SerializeField] private LayerMask jumpableGround;
private float dirX = 0f;
[SerializeField]private float PlayerMovementSpeed = 7f;
[SerializeField]private float PlayerJumpSpeed = 8f;
private enum MovementState { idle, running, jumping, falling }
// Start is called before the first frame update
private void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
collider = GetComponent<BoxCollider2D>();
sprite = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal");
rigidbody.velocity = new Vector2(dirX * PlayerMovementSpeed, rigidbody.velocity.y);
if (Input.GetButtonDown("Jump") && IsGrounded())
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, PlayerJumpSpeed);
}
UpdateAnimationState();
}
private void UpdateAnimationState()
{
MovementState state;
if (dirX > 0f)
{
state = MovementState.running;
sprite.flipX = false;
}
else if (dirX < 0f)
{
state = MovementState.running;
sprite.flipX = true;
}
else
{
state = MovementState.idle;
}
if (rigidbody.velocity.y > .1f)
{
state = MovementState.jumping;
}
else if (rigidbody.velocity.y < -.1f)
{
state = MovementState.falling;
}
animator.SetInteger("state", (int)state);
}
private bool IsGrounded()
{
return Physics2D.BoxCast(collider.bounds.center, collider.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
}
在代码中实现这一点的简单方法是添加一个跳转计数器,变量从 0 开始,每次跳转时将值加 1,然后当角色在地面上时重置 de 变量。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rigidbody;
private BoxCollider2D collider;
private SpriteRenderer sprite;
private Animator animator;
[SerializeField] private LayerMask jumpableGround;
private float dirX = 0f;
[SerializeField]private float PlayerMovementSpeed = 7f;
[SerializeField]private float PlayerJumpSpeed = 8f;
private enum MovementState { idle, running, jumping, falling }
// NEW VARIABLE
private int jumpCounter;
// Start is called before the first frame update
private void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
collider = GetComponent<BoxCollider2D>();
sprite = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
jumpCounter = 0;
}
// Update is called once per frame
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal");
rigidbody.velocity = new Vector2(dirX * PlayerMovementSpeed, rigidbody.velocity.y);
//THE CONDITION CHANGES
if (Input.GetButtonDown("Jump") && jumpCounter<2)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, PlayerJumpSpeed);
jumpCounter++;
}
if(IsGrounded())
{
jumpCounter = 0;
}
enter code here
UpdateAnimationState();
}
private void UpdateAnimationState()
{
MovementState state;
if (dirX > 0f)
{
state = MovementState.running;
sprite.flipX = false;
}
else if (dirX < 0f)
{
state = MovementState.running;
sprite.flipX = true;
}
else
{
state = MovementState.idle;
}
if (rigidbody.velocity.y > .1f)
{
state = MovementState.jumping;
}
else if (rigidbody.velocity.y < -.1f)
{
state = MovementState.falling;
}
animator.SetInteger("state", (int)state);
}
private bool IsGrounded()
{
return Physics2D.BoxCast(collider.bounds.center, collider.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
}
编辑:添加了对 IsGrounded 方法的修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.