这里是翻译后的内容:
哎呀,我在做一个2D游戏,游戏中有一个大型的管理/城市建造部分,我想让玩家能够使用WASD或鼠标指针位于屏幕边缘来控制摄像机。但是,无论我怎么做,摄像机都只向下移动,指针的位置无关紧要。这里是我的代码(Move函数正常工作,EdgeMove中的“ctx”是指针位置):
[SerializeField]
private float speed;
[SerializeField]
private int screenEdge;
private Vector2 moveInput;
private Rigidbody2D rb;
// Start是MonoBehaviour创建后第一次执行Update方法
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update是每一帧执行一次
void Update()
{
rb.linearVelocity = moveInput * speed;
}
public void Move(InputAction.CallbackContext ctx)
{
moveInput = ctx.ReadValue<Vector2>();
}
public void EdgeMove(InputAction.CallbackContext ctx)
{
print(Camera.main.ScreenToWorldPoint(ctx.ReadValue<Vector2>()));
if (Camera.main.ScreenToWorldPoint(ctx.ReadValue<Vector2>()).y < screenEdge)
{
moveInput.y = -1f;
}
if (Camera.main.ScreenToWorldPoint(ctx.ReadValue<Vector2>()).y > Screen.height - screenEdge)
{
moveInput.y = +1f;
}
if (Camera.main.ScreenToWorldPoint(ctx.ReadValue<Vector2>()).x < screenEdge)
{
moveInput.x = -1f;
}
if (Camera.main.ScreenToWorldPoint(ctx.ReadValue<Vector2>()).x > Screen.height - screenEdge)
{
moveInput.x = +1f;
}
}
我正在使用Unity 6.5和新的输入系统,找不到任何教程,教程都针对3D游戏或使用旧的输入系统。如果您有任何帮助、建议或解决方案,请帮忙!(对不起,如果我的拼写和语法不标准,英语不是我的母语)
评论 (0)