【发布时间】:2022-11-23 20:46:36
【问题描述】:
我有一个脚本来统一移动我的角色(玩家)。脚本很好,没有任何错误,虽然当我输入 播放模式并尝试使用箭头移动我的角色,它根本不动,我不知道是什么问题。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 1f;
public float CollisionOffset = 0.05f;
public ContactFilter2D movementFilter;
Vector2 movementInput;
Rigidbody2D rb;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate() {
if (movementInput != Vector2.zero) {
int count = rb.Cast(
movementInput,
movementFilter,
castCollisions,
moveSpeed * Time.fixedDeltaTime + CollisionOffset
);
if (count == 0) {
rb.MovePosition(rb.position + movementInput * moveSpeed * Time.fixedDeltaTime);
}
}
}
void onMove(InputValue movementValue) {
movementInput = movementValue.Get<Vector2>();
}
}
统一版本:2022.2.0b14
输入系统:1.2.0版
任何帮助表示赞赏。
【问题讨论】:
-
“onMove”方法在执行时,因为从我看到你的 movementInput 它将等于 Vector2.Zero 因为当你创建引用时它会自动为零。
-
@PavlosMavris 你能详细说明一下吗?
-
除非执行“onMove”方法,否则您的“movementInput”将为零。我想你的 onMove 方法会在你的玩家开始移动时执行?
标签: c# unity3d 2d game-development