【发布时间】:2021-03-04 08:52:20
【问题描述】:
我正在使用 Unity 制作一款 2D 平台游戏,自从我决定使用新的输入系统来支持游戏手柄和键盘以来已经 3 周了,但我仍在苦苦挣扎。另外,我正在使用 Player Input 组件并调用统一事件行为。事实上,我从 GitHub 下载了一个播放器控制器代码以将其用作指南,并将其复制并粘贴到我的实际播放器控制器中,但问题是它使用的是旧的输入系统。所以我需要稍微更改一下代码,但我不知道如何在 void update 而不是(Input.GetAxisRaw("Horizontal")) 中编写等效代码。我一直在努力解决这个问题,如果您能指导我,我将不胜感激。
这是我的播放器控制器代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerInputActions controls;
private Rigidbody2D rb;
private Animator anim;
private bool facingRight = true;
private float moveInput;
public Transform feetPos;
public float jumpInput;
public float speed;
[SerializeField] float JumpVelocity = 5;
float JumpPressedRemember = 0;
[SerializeField] float JumpPressedRememberTime = 0.2f;
float GroundedRemember = 0;
[SerializeField] float GroundedRememberTime = 0.25f;
[SerializeField] float HorizontalAcceleration = 1;
[SerializeField] [Range(0, 1)] float HorizontalDampingBasic = 0.5f;
[SerializeField] [Range(0, 1)] float HorizontalDampingWhenStopping = 0.5f;
[SerializeField] [Range(0, 1)] float HorizontalDampingWhenTurning = 0.5f;
[SerializeField] [Range(0, 1)] float JumpHeight = 0.5f;
private void Awake()
{
controls = new PlayerInputActions();
}
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
public void OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<float>();
}
public void OnJump(InputAction.CallbackContext context)
{
JumpVelocity = context.ReadValue<float>();
}
void FixedUpdate()
{
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void Update()
{
Vector2 GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
Vector2 GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
bool Grounded = Physics2D.OverlapBox(GroundedBoxCheckPosition, transform.localScale, 0);
GroundedRemember -= Time.deltaTime;
if (Grounded)
{
GroundedRemember = GroundedRememberTime;
}
JumpPressedRemember -= Time.deltaTime;
if (controls.Player.Jump.triggered)
{
JumpPressedRemember = JumpPressedRememberTime;
}
if (controls.Player.Jump.triggered)
{
if (rb.velocity.y > 0)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * JumpHeight);
}
}
if ((JumpPressedRemember > 0) && (GroundedRemember > 0))
{
JumpPressedRemember = 0;
GroundedRemember = 0;
rb.velocity = new Vector2(rb.velocity.x, JumpVelocity);
}
float HorizontalVelocity = rb.velocity.x;
HorizontalVelocity += Input.GetAxisRaw("Horizontal");
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.01f)
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenStopping, Time.deltaTime * 10f);
else if (Mathf.Sign(Input.GetAxisRaw("Horizontal")) != Mathf.Sign(HorizontalVelocity))
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenTurning, Time.deltaTime * 10f);
else
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingBasic, Time.deltaTime * 10f);
rb.velocity = new Vector2(HorizontalVelocity, rb.velocity.y);
}
}
这是我从 GitHub 下载的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
LayerMask lmWalls;
[SerializeField]
float fJumpVelocity = 5;
Rigidbody2D rigid;
float fJumpPressedRemember = 0;
[SerializeField]
float fJumpPressedRememberTime = 0.2f;
float fGroundedRemember = 0;
[SerializeField]
float fGroundedRememberTime = 0.25f;
[SerializeField]
float fHorizontalAcceleration = 1;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingBasic = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenStopping = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenTurning = 0.5f;
[SerializeField]
[Range(0, 1)]
float fCutJumpHeight = 0.5f;
void Start ()
{
rigid = GetComponent<Rigidbody2D>();
}
void Update ()
{
Vector2 v2GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
Vector2 v2GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
bool bGrounded = Physics2D.OverlapBox(v2GroundedBoxCheckPosition, v2GroundedBoxCheckScale, 0, lmWalls);
fGroundedRemember -= Time.deltaTime;
if (bGrounded)
{
fGroundedRemember = fGroundedRememberTime;
}
fJumpPressedRemember -= Time.deltaTime;
if (Input.GetButtonDown("Jump"))
{
fJumpPressedRemember = fJumpPressedRememberTime;
}
if (Input.GetButtonUp("Jump"))
{
if (rigid.velocity.y > 0)
{
rigid.velocity = new Vector2(rigid.velocity.x, rigid.velocity.y * fCutJumpHeight);
}
}
if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0))
{
fJumpPressedRemember = 0;
fGroundedRemember = 0;
rigid.velocity = new Vector2(rigid.velocity.x, fJumpVelocity);
}
float fHorizontalVelocity = rigid.velocity.x;
fHorizontalVelocity += Input.GetAxisRaw("Horizontal");
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.01f)
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenStopping, Time.deltaTime * 10f);
else if (Mathf.Sign(Input.GetAxisRaw("Horizontal")) != Mathf.Sign(fHorizontalVelocity))
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenTurning, Time.deltaTime * 10f);
else
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingBasic, Time.deltaTime * 10f);
rigid.velocity = new Vector2(fHorizontalVelocity, rigid.velocity.y);
}
}
【问题讨论】:
-
应该
OnJump设置跳跃速度吗?看起来你应该有一个布尔值设置为true,例如jumped。然后在您的计算中使用它而不是控件上的triggered属性。在使用控件时,我遇到了很多期望与实际的问题。如果你能确认你的意图,我可以提供一个官方样本。 -
Input.GetAxisRaw("Horizontal");也应更改为moveInput.X;。不应使用 GetAxisRaw,因为您已经从OnMove方法中获取了移动值。 -
感谢您的回复。我想也许我应该在“OnJump”中使用“jumpInput”而不是“JumpVelocity”。这段代码中的所有内容都搞砸了。事实上,我从 Git Hub 下载了一个播放器控制器代码以将其用作指南,但问题是它使用的是旧输入系统。尽管如此,我还是复制了该代码并将其粘贴到该代码中。这就是为什么这里的一切都很复杂。我之前添加了“jumpInput”,但“JumpVelocity”是针对其他播放器控制器的。
-
你能给我看一个布尔值的例子吗?我明白你的意思,但我想写正确,所以我问。
-
@ps2goat 另外,我更改了 Input.GetAxisRaw("Horizontal");移动Input.X;但我收到一条错误消息,提示“float”不包含“x”的定义。如何定义“x”?