c# - Unity 2D 跳转脚本
问题描述
所以我试图在我的游戏中实现双跳,但这是行不通的。而现在,不知何故,我的球员不仅不能二段跳,他们甚至连跳也不能!
更新:他们现在可以跳,但仍然不能二段跳。
这是我的整个运动脚本:
using UnityEngine;
namespace Players
{
public class Actor : MonoBehaviour
{
//in order to control both players using 1 script.
public int playerIdx;
//Variables.
public float movementSpeed = 150f;
public float jumpForce = 250f;
//Ground stuff.
public LayerMask whatIsGround;
public bool grounded;
//boolean stuff.
private bool facingRight;
private bool moving;
//Needed to check if player is on the ground.
public Transform groundCheck;
//Limit player's movement speed.
public float maxMovementSpeed = 400f;
//Double jump stuff.
private bool doubleJumpReady;
//rb
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
doubleJumpReady = true;
rb = GetComponent<Rigidbody2D>();
facingRight = true;
}
// Update is called once per frame
void FixedUpdate()
{
SlowDown();
}
private void LateUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, whatIsGround);
if (grounded)
doubleJumpReady = true;
}
private void SlowDown()
{
if (moving) return;
//if player is not moving, slow them down.
if (rb.velocity.x > 0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * -Vector2.right);
if (rb.velocity.x < -0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right);
}
public void Move(int dir)
{
//Flip the player.
Flip(dir);
//Moving the player.
moving = true;
float xVel = rb.velocity.x; //Get x velocity.
if ( dir > 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir < 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir == 0) { } //do nothing.
//Help player turn around faster.
if (xVel > 0.2f && dir < 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * -Vector2.right);
if (xVel < 0.2f && dir > 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * Vector2.right);
}
private void Flip(int dir)
{
if (facingRight && dir == -1 || !facingRight && dir == 1)
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
}
}
不知道是因为我的跳转脚本,还是我的播放器脚本:
void Update()
{
if (playerIdx == 1)
{
if (Input.GetKey(KeyCode.A))
Move(-1);
if (Input.GetKey(KeyCode.D))
Move(1);
if (Input.GetKey(KeyCode.W))
Jump();
}
if (playerIdx == 2)
{
if (Input.GetKey(KeyCode.LeftArrow))
Move(-1);
if (Input.GetKey(KeyCode.RightArrow))
Move(1);
if (Input.GetKey(KeyCode.UpArrow))
Jump();
}
}
那么我该如何解决这个问题呢?
解决方案
据我所知,你从来没有重置过
doubleJumpReady = false;
多变的。要解决此问题,只需将跳转代码更改为:
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
希望它有效;)。
编辑:接地由重叠的球体设置。因此无需在此处设置。使用此代码并按两次跳转 btn 并查看 Debug.Log 消息是否显示。此外,您的玩家 ID(不需要 idx。)据我所知,您的脚本将两个附加到不同的对象。因此,它们的变量无论如何都不会共享。
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
Debug.Log("I am double jumping");
}
}
最后一个问题是,你不会同时执行你的一个跳转。这是由于您的执行而发生的。
Input.GetKey(KeyCode.UP)
改为使用:
Input.GetKeyDown(KeyCode.Up);
GetKeyDown 在按下按钮时返回 true。GetKey 在按下按钮时返回 true。
希望它现在可以工作;)
推荐阅读
- java-8 - 多个@EnsuresNonNullIf 应该没问题;但编译器抱怨
- javascript - 为什么 Jest 快照会因为双引号而包含转义字符?
- sql-server - “将表达式转换为数据类型 nvarchar 的算术溢出错误”:pypyodbc + SQL Server
- ruby - 找不到结束输入丢失的位置
- django - 有没有办法让鹡鸰身体字段只读?
- android - 从 Android 应用程序访问 i.mx6 板上的 I2C
- javascript - 一个人如何使用不和谐机器人将频道添加到某个类别?
- jquery - 通过 JQuery 使用 SlideDown 和 SlideUp 更改图标
- c++ - 有人可以解释一下,为什么这段代码不需要函数定义?
- c# - DataGrid datagridComboxColumn 不显示值