Unity中移动平台的奇怪输出 - c#

首先,很抱歉,它的编写效果不佳,我花了很多时间调试它,并感到非常压力。我试图建立一个可以在航路点之间移动的统一移动平台,但我不想让世界上大量的游戏对象占用宝贵的处理能力,因此我正在尝试使用我可以只需通过编辑器将其添加到脚本中即可。

唯一的问题是它似乎以惊人的速度执行此操作:
Unity中移动平台的奇怪输出 - c#
黑色=摄像机视图,蓝色=平台以及基于航点的行驶方向,红色=当前正在执行的操作。

我已经花了数小时试图找到解决方法,但是我不知道为什么要这样做。

我在平台上的脚本:

public Vector3[] localWaypoints;
    Vector3[] globalWaypoints;

    public float speed;
    public bool cyclic;
    public float waitTime;
    [Range(0, 2)]
    public float easeAmount;

    int fromWaypointIndex;
    float percentBetweenWaypoints;
    float nextMoveTime;

    void Start()
    {

        globalWaypoints = new Vector3[localWaypoints.Length];
        for (int i = 0; i < localWaypoints.Length; i++)
        {
            globalWaypoints[i] = localWaypoints[i] + transform.position;
        }
    }

    void Update()
    {


        Vector3 velocity = CalculatePlatformMovement();
        transform.Translate(velocity);
    }

    float Ease(float x)
    {
        float a = easeAmount + 1;
        return Mathf.Pow(x, a) / (Mathf.Pow(x, a) + Mathf.Pow(1 - x, a));
    }

    Vector3 CalculatePlatformMovement()
    {

        if (Time.time < nextMoveTime)
        {
            return Vector3.zero;
        }

        fromWaypointIndex %= globalWaypoints.Length;
        int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length;
        float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]);
        percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints;
        percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints);
        float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints);

        Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints);

        if (percentBetweenWaypoints >= 1)
        {
            percentBetweenWaypoints = 0;
            fromWaypointIndex++;

            if (!cyclic)
            {
                if (fromWaypointIndex >= globalWaypoints.Length - 1)
                {
                    fromWaypointIndex = 0;
                    System.Array.Reverse(globalWaypoints);
                }
            }
            nextMoveTime = Time.time + waitTime;
        }

        return newPos - transform.position;
    }


    struct PassengerMovement
    {
        public Transform transform;
        public Vector3 velocity;
        public bool standingOnPlatform;
        public bool moveBeforePlatform;

        public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standingOnPlatform, bool _moveBeforePlatform)
        {
            transform = _transform;
            velocity = _velocity;
            standingOnPlatform = _standingOnPlatform;
            moveBeforePlatform = _moveBeforePlatform;
        }
    }

    void OnDrawGizmos()
    {
        if (localWaypoints != null)
        {
            Gizmos.color = Color.red;
            float size = .3f;

            for (int i = 0; i < localWaypoints.Length; i++)
            {
                Vector3 globalWaypointPos = (Application.isPlaying) ? globalWaypoints[i] : localWaypoints[i] + transform.position;
                Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
                Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
            }
        }
    }

更新:经过进一步测试,我发现如果将localWaypoint数组中的第一个对象设置为0,0,0,将我的第二个对象设置为1,0,0,则平台将向右旋转,确保命中不断旋转的航路点,然后像上面的图像一样向远处扩展。但是,如果我将第一个对象设置为0,0,0,将第二个对象设置为-1,0,0,则该对象的行为将与以前相同,但是将向左旋转,如图所示。 (第二张图片也进行了更新,以显示platfrom如何确保在螺旋成螺旋形之前没有到达两个航点)。

Unity中移动平台的奇怪输出 - c#

我还注意到,如果将两个航路点都设置为0,0,0,则平台保持静止,这两个事实证明它与航路点的处理方式有关,而没有其他脚本或父对象的干扰。

参考方案

在我的测试应用中,可以使用更新后的数字([0,0,0],[1,0,0])。但是,如果在对象的Y轴上旋转,则会看到与您所看到的行为相同的行为。在更新中,如果您更改:

transform.Translate(velocity);

transform.Translate(velocity, Space.World); 

您应该看到所需的行为。注意,“ transform.Translate(velocity)”与“ transform.Translate(velocity,Space.Self)”相同。您的翻译正在旋转。

如果您好奇,请查看此内容以获取有关如何应用转换中的值的更多信息:
https://gamedev.stackexchange.com/questions/138358/what-is-the-transformation-order-when-using-the-transform-class

TypeError:_transform()接受2个位置参数,但给出了3个 - python

我了解了CaesarCipher:In [90]: !cat caesar_cipher.py class CaesarCipher: """Construct Caesar cipher using given integer shift for rotation.""" def __init__…

LeetCode题解计算机为什么是基于二进制的?

可以是三进制么?二进制有什么好处?题解:为什么叫电子计算机?算盘应该没有二进制

LeetCode题解统计城市的所有灯泡

这个是我刚毕业的时候,一个真实的面试题,这是一个开放题。题目描述:想办法,将一个城市的所有灯泡数量统计出来。题解:费米估算法1、如果某个城市常驻人口有1000万2、假设每5人居住在一套房里,每套房有灯泡5只,那么住宅灯泡共有1000万只3、假设公众场所每10人共享一只灯泡,那么共有100万只4、主要的这两者相加就得出了1100万只当然实际上这是估算的,具体应…

LeetCode题解黑白圆盘

一个圆盘被涂上了黑白二色,两种颜色各占一个半圆。圆盘以一个未知的速度、按一个未知的方向旋转。你有一种特殊的相机可以让你即时观察到圆上的一个点的颜色。你需要多少个相机才能确定圆盘旋转的方向?题解:可以用一个相机即可

LeetCode题解圆上任取三点构成锐角三角形的概率

来自字节跳动的一道几何题题解:1/4