C# } 如果((fJumpPressedMember>0)和&(fGroundedMember>0)) { FJumpPressedMember=0; fGroundedRemember=0; 刚性.velocity=新矢量2(刚性.velocity.x,FvVelocity); } 浮动f水平速度=刚性速度x; fHorizontalVelocity+=Input.GetAxisRaw(“水平”); if(Mathf.Abs(Input.GetAxisRaw(“水平”))输入中编辑它
从unity文档和我对unity的一点了解来看,似乎您使用它是正确的,但是在示例中,它们运行C# } 如果((fJumpPressedMember>0)和&(fGroundedMember>0)) { FJumpPressedMember=0; fGroundedRemember=0; 刚性.velocity=新矢量2(刚性.velocity.x,FvVelocity); } 浮动f水平速度=刚性速度x; fHorizontalVelocity+=Input.GetAxisRaw(“水平”); if(Mathf.Abs(Input.GetAxisRaw(“水平”))输入中编辑它,c#,unity3d,input,2d-games,C#,Unity3d,Input,2d Games,从unity文档和我对unity的一点了解来看,似乎您使用它是正确的,但是在示例中,它们运行float speed=Input.GetAxisRaw(“水平”)*Time.deltaTime设置->输入中编辑它 文档:从unity文档和我对unity的一点了解来看,似乎您使用它是正确的,但是在示例中,它们运行float speed=Input.GetAxisRaw(“水平”)*Time.deltaTime设置->输入中编辑它 文档:我想尝试一下,因为我还没有在自己的游戏中实现跳跃,我想看看新的输
float speed=Input.GetAxisRaw(“水平”)*Time.deltaTime因此,如果您遇到问题,请尝试使用该行,看看有什么变化。如果您正在努力进行键映射,它也会说您可以在编辑->设置->输入中编辑它
文档:从unity文档和我对unity的一点了解来看,似乎您使用它是正确的,但是在示例中,它们运行float speed=Input.GetAxisRaw(“水平”)*Time.deltaTime因此,如果您遇到问题,请尝试使用该行,看看有什么变化。如果您正在努力进行键映射,它也会说您可以在编辑->设置->输入中编辑它
文档:我想尝试一下,因为我还没有在自己的游戏中实现跳跃,我想看看新的输入系统有多简单。我很喜欢这一点,基于事件的输入最酷的部分之一是,您不必将所有内容都放在一个方法中
请注意,我是在3D中完成这项工作的——我还没有设置2D游戏。我假设您可能需要调整施加力的轴,以及使用RigidBody2D和2D碰撞器
对于设置,您的地形上需要一个碰撞器,角色上需要一个碰撞器和刚体。(我假设你已经设置好了,但这是为其他找到答案的人准备的。)需要在地形上安装对撞机,这样玩家就不会跌入地形。播放器上的对撞机也是出于同样的原因。刚体允许围绕重力和质量等进行物理和计算(本例中必须启用重力!)
因此,这个例子将允许我们向上方向对玩家施加一个力,然后重力将角色带回地形
首先,请注意,当您创建控件并自动生成一个C#类时,您需要让您的类继承它。您的控件类称为PlayerInputActions
。我没有看到“行动地图”的名字,所以我会用我的名字。在GUI中,我的称为“故障”。这个动作叫做“跳跃”。如果检查自动生成的C#类,您将找到名称空间、接口和方法。我的动作图在生成时成为一个接口,名为IAvatarDefaultActions
。既然你没有列出你的ActionMap名字,我就用我的名字。在代码中将其替换为您的
必要时导入命名空间,并继承播放器输入类上的接口
为了简洁起见,删除了一些代码
让我知道这是否有帮助。如果没有,我可以尝试设置2D环境。对于2D来说,唯一的改变似乎是使用ForceMode2D.Force
作为参考,这是我的输入设置的样子。我将“跳跃”设置为“按钮”动作类型,因为我只关心它是否被按下。
移动修复更新
至于移动问题
,输入系统旨在轻松传递用于斗杆和D-pad移动的矢量2。因此,通过的值将分别具有movement.x
和movement.y
以获得水平和垂直输入
以下是我设置移动动作的方式:
这就是我将Up
控件映射到W
键的方式。(其他键类似,但映射到相应的键。)
这是我设置移动变量的代码
private Vector2 moveInput;
void PlayerInputActions.IAvatarDefaultActions.OnMove(InputAction.CallbackContext context)
{
moveInput= context.ReadValue<Vector2>();
}
私有向量2移动输入;
void PlayerInputActions.IAvatarDefaultActions.OnMove(InputAction.CallbackContext上下文)
{
moveInput=context.ReadValue();
}
现在您只需在水平值的更新代码中使用moveInput.x
我想尝试一下,因为我还没有在自己的游戏中实现跳跃,我想看看新的输入系统有多简单。我很喜欢这一点,基于事件的输入最酷的部分之一是,您不必将所有内容都放在一个方法中
请注意,我是在3D中完成这项工作的——我还没有设置2D游戏。我假设您可能需要调整施加力的轴,以及使用RigidBody2D和2D碰撞器
对于设置,您的地形上需要一个碰撞器,角色上需要一个碰撞器和刚体。(我假设你已经设置好了,但这是为其他找到答案的人准备的。)需要在地形上安装对撞机,这样玩家就不会跌入地形。播放器上的对撞机也是出于同样的原因。刚体允许围绕重力和质量等进行物理和计算(本例中必须启用重力!)
因此,这个例子将允许我们向上方向对玩家施加一个力,然后重力将角色带回地形
首先,请注意,当您创建控件并自动生成一个C#类时,您需要让您的类继承它。您的控件类称为PlayerInputActions
。我看不到“行动”
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);
}
}
public class PlayerController : MonoBehaviour, PlayerInputActions.IAvatarDefaultActions {
// allow changing the force applied when jumping in the editor.
// note that for a mass of 1 on my character, I had to use a value of about 300
[SerializeField]
private float jumpForce;
// track our instance of controls
private PlayerInputActions controls;
// in awake, we need to create a new instance of controls
// and bind the action events.
private void Awake()
{
controls = new PlayerInputActions();
// the auto-generated class takes care of all the event registration we need to do!
// without this, we won't get event notifications.
controls.AvatarDefault.SetCallbacks(this);
}
// I chose to explicitly implement the interface for clarity (since I'm still newish)
void PlayerInputActions.IAvatarDefaultActions.OnJump(InputAction.CallbackContext context)
{
// there are several phases, but the only one we should care about is whether
// the action was performed or not. (You can use an if statement, if you want)
switch (context.phase)
{
case InputActionPhase.Performed:
// to separate event handlers from actual code, I've been putting the
// actual logic in a separate method
this.Jump();
break;
}
}
public void Jump()
{
// you can play around with the ForceMode here, and probably need
// to use a Vector2 since you're working with 2D.
// in this example, though, it's applying the jump force on the y axis.
// a positive value will make the character thrust upward.
rb.AddForce(transform.up * this.jumpForce, ForceMode.Force);
}
// You should also have these two methods
public void OnEnable()
{
controls.Enable();
}
public void OnDisable()
{
controls.Disable();
}
}
private Vector2 moveInput;
void PlayerInputActions.IAvatarDefaultActions.OnMove(InputAction.CallbackContext context)
{
moveInput= context.ReadValue<Vector2>();
}