【问题标题】:Unity Player character is not movingUnity Player角色不动
【发布时间】: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


【解决方案1】:

OnMove 未执行,因为它是 OnMove 而不是 onMove。这是新输入系统直接引用的。所以函数名称应该是 On+[ActionName] 并且在这里当你使用默认 InputActions 时移动 -> OnMove。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多