【问题标题】:How can I properly create invalid Vector3 in Unity?如何在 Unity 中正确创建无效的 Vector3?
【发布时间】:2014-11-11 16:11:32
【问题描述】:

我经常需要将无效位置传递给可选择使用位置的函数(例如 RTS 单元操作)。但这不会编译:

public abstract void CastSpell(Spell spell, Vector3 targetLocation = null);

那么我怎样才能制作一个合适的 invaid Vector3 来确定无效/“不关心”的位置?我见过像带有 -999999 坐标的向量之类的东西 - 但这是一个可能导致错误的讨厌技巧(例如,在巨大的地图上)。

【问题讨论】:

    标签: unity3d optional-parameters


    【解决方案1】:

    我过去也遇到过类似的问题,我发现有两种解决方案。第一个解决方案是为该类创建一个包装器。

    Class Vector3Wrapper
    {
        Vector3 vector;
    }
    

    然后我只需将 Vector3Wrapper 设置为 null,然后如果它不是 null,则访问其向量。更好的方法是将其设为 Nullable 类型。下面是一个例子。

    http://unitypatterns.com/nullable-types/

    public class Character : MonoBehaviour 
    {
        //Notice the added "?"
        Vector3? targetPosition;
     
        void MoveTowardsTargetPosition()
        {
            //First, check if the variable has been assigned a value
            if (targetPosition.HasValue)
            {
                //move towards targetPosition.Value
            }
            else
            {
                //targetPosition.Value is invalid! Don't use it!
            }
        }
    

    【讨论】:

    • 你能扩展你的答案吗?我不希望你失去一个好的答案,因为它只是一个链接的答案。
    • 我把它扩展了一点,我只是不想重写网站上的信息并声称它是我自己的。
    • +1。别担心,只要你把它建好就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多