首页 > 解决方案 > Unity没有运行基本脚本

问题描述

我是第一次观看有关制作 2d、自上而下游戏的教程的程序员。该教程稍有过时,但只有几年的时间,所以我认为它会起作用。我对这段特定代码的目标只是让玩家在按下 A 时向左看,在按下 D 时向右看。现在,什么都没有发生,但我在控制台中看不到任何错误。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    private BoxCollider2D boxCollider;

    private Vector3 moveDelta;

    private void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
    }

    private void FixedUpdated()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        //Reset moveDelta
        moveDelta = new Vector3(x, y, 0);

        //Swap sprite direction for left or right
        if (moveDelta.x > 0)
            transform.localScale = Vector3.one;
        else if (moveDelta.x < 0)
            transform.localScale = new Vector3(-1, 1, 1);
    }
}

我的 Unity 设置是否错误,或者我的代码中是否有错误?

我正在关注的教程是在 Udemy 上“通过创建真正的自上而下 RPG 来学习 Unity 引擎和 C#”,我没有看到其他人有同样的问题。

任何帮助表示赞赏:)

标签: c#unity3d

解决方案


Unity3D 与一些必须完全匹配某些预定义名称的函数一起使用。

你写了“ FixedUpdated”,这不是 Unity 知道的函数,所以它永远不会“自动”调用它。

你可能把它误认为FixedUpdate是 Unity 每帧调用的函数。

请注意,不幸的是,编译器永远不会为此引发错误。

但是,在 Visual Studio 中会有关于此的消息。我强烈建议检查选项卡“错误”,默认情况下位于底部,并取消过滤“信息”消息,然后阅读它们。:

IDE0051   Private member 'NewBehaviourScript.FixedUpdated' is unused

Visual Studio 2019 显示未使用 FixedUpdated 的消息


推荐阅读