c# - Unity 新的输入系统和双跳本地多人游戏
问题描述
所以我在我目前正在开发的游戏中使用新的输入系统时遇到了一些麻烦。游戏将是某种类型的 Smash 游戏,我已经用旧的输入系统实现了二段跳,它可以工作,但现在我已经将其更改为新的,我可以再次在 Air 中跳一次我知道它一定是某种容易解决的问题,但我是初学者,我不太了解新的 I-System。这是我的代码:如果您能帮助我解决这个问题,我将非常感激,最终我可以从您的评论中学到一些东西。(顺便说一句。对不起我的英语我不是本地人哈哈)
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpForce;
public float moveInput;
public float checkRadius;
private int extraJumps;
public int extraJumpsValue;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public LayerMask whatIsGround;
private Vector2 movementInput = Vector2.zero;
private bool jumped = false;
//OnMove Input to later set as Event in Input Manager
public void OnMove(InputAction.CallbackContext context)
{
movementInput = context.ReadValue<Vector2>();
}
//OnJump Input to later set as Event in Input Manager
public void OnJump(InputAction.CallbackContext context)
{
jumped = context.action.triggered;
}
//start
private void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
//check if isGrounded
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius,
whatIsGround);
moveInput = movementInput.x;
//Move Right/Left relative to moveInput Value (-1 till 1)
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
//rotate Player Sprite relative to a / d Input
if(facingRight == false && moveInput > 0)
{
Flip();
}
else if(facingRight == true && moveInput <0)
{
Flip();
}
}
private void Update()
{
// if grounded set extra jumps u can take to a preset value
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
}
//if extrajumps is equal to 0 & player tries to jump and is grounded add upwards velocity
if (extraJumps == 0 && jumped && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
//subtract extra jump -1 and "jump"
else if (extraJumps > 0 && jumped)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
}
//rotate player
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}
解决方案
我认为问题在于执行第一次跳转的条件:
//When isGrouded then extraJumps equal 1 (in the case extraJumpsValue is 1)
if(isGrounded == true)
{
extraJumps = 1;
}
//The next condition is always false
//Because when isGrouded then extraJumps is never 0
if (isGrounded == true && extraJumps == 0 && /*Some other condition*/)
...
我不明白为什么extraJumps
要检查第一次跳跃。我认为您可以删除此操作数:
if (/*extraJumps == 0 &&*/ jumped && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
完整代码:
private void Update()
{
// if grounded set extra jumps u can take to a preset value
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
}
//if player tries to jump and is grounded add upwards velocity
if (jumped && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
//subtract extra jump -1 and "jump"
else if (extraJumps > 0 && jumped)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
}
推荐阅读
- java - 存储库注入未被识别为 bean
- python - 在 Python 中使用 API / WebService 而不是 C#
- amazon-cloudformation - 如何创建带有云形成模板附加策略的角色?
- python-3.x - 根据列的唯一和重复值从现有数据框创建两个新数据框
- python - How to set schema in Python to use a json file on BigQuery?
- java - Why am I not able to fetch the HTML content from Lotus Notes using JavaMail API?
- java - 如何在android中设置TextInputEditText的图标右侧和底部
- python - 为什么“if”检查不能防止字符串中的 IndexError?
- mysql - 显示名称和扩展名,其中首都是表 CITY 中国家/地区名称的扩展名
- angular - Ionic/Angular 按钮单击:ctx.xxx 不是函数