예제 #1
0
    public Vector3 getSteering(Rigidbody target, Vector3 offset, out Vector3 targetPos)
    {
        // 得到世界坐标的偏移位置
        Vector3 worldOffsetPos = target.position + target.transform.TransformDirection(offset);

        Debug.DrawLine(transform.position, worldOffsetPos);

        /* 计算距离到偏移点 */
        Vector3 displacement = worldOffsetPos - transform.position;
        float   distance     = displacement.magnitude;


        float speed = rb.velocity.magnitude;

        /* 预测的距离不要超过当前距离 */
        float prediction;

        if (speed <= distance / maxPrediction)
        {
            prediction = maxPrediction;
        }
        else
        {
            prediction = distance / speed;
        }

        /* 目标位置 */
        targetPos = worldOffsetPos + target.velocity * prediction;

        return(steeringBasics.Arrive(targetPos));
    }
예제 #2
0
    public Vector3 GetSteering(Rigidbody target, ICollection <Rigidbody> obstacles, out Vector3 bestHidingSpot)
    {
        // 临时值
        float distToClostest = Mathf.Infinity;

        bestHidingSpot = Vector3.zero;

        // 找到最近的隐藏点 , 遍历所有障碍
        foreach (Rigidbody r in obstacles)
        {
            // 这个障碍  可以作为隐蔽的位置
            Vector3 hidingSpot = GetHidingPosition(r, target);

            // 离我多远
            float dist = Vector3.Distance(hidingSpot, transform.position);

            // 最近就保存
            if (dist < distToClostest)
            {
                distToClostest = dist;
                bestHidingSpot = hidingSpot;
            }
        }

        //如果没有发现隐藏点,就只躲避敌人    (比如我和敌人都处于所有障碍的一侧)
        if (distToClostest == Mathf.Infinity)
        {
            return(evade.GetSteering(target));
        }

        Debug.DrawLine(transform.position, bestHidingSpot);

        // 返回加速度
        return(steeringBasics.Arrive(bestHidingSpot));
    }
예제 #3
0
    public Vector3 GetSteering(ICollection <Rigidbody> targets)
    {
        Vector3 centerOfMass = Vector3.zero;
        int     count        = 0;

        /* 得到我前方视野内所有角色的  中心 */
        foreach (Rigidbody r in targets)
        {
            // 在视野内  (视野是  无限扇形)
            if (steeringBasics.isFacing(r.position, facingCosineVal))
            {
                centerOfMass += r.position;
                count++;
            }
        }

        if (count == 0)   // 我前面没有人。 漫游好了
        {
            return(Vector3.zero);
        }
        else
        {
            // 目标目标位置
            centerOfMass = centerOfMass / count;

            return(steeringBasics.Arrive(centerOfMass));
        }
    }
예제 #4
0
    // Update is called once per frame
    void Update()
    {
        Vector3 accel = steeringBasics.Arrive(targetPosition);

        steeringBasics.Steer(accel);
        steeringBasics.LookWhereYoureGoing();
    }
예제 #5
0
    public Vector3 getSteering(LinePath path, bool pathLoop, out Vector3 targetPosition)
    {
        // 如果路径只有一个节点, 那么只需转到该位置
        if (path.Length == 1)
        {
            targetPosition = path[0];
        }
        //否则, 在该路径上找到最接近的点, 然后转到该位置。
        else
        {
            if (!pathLoop)
            {
                /* 查找此路径上的 最终目标 */
                Vector2 finalDestination = (pathDirection > 0) ? path[path.Length - 1] : path[0];

                // 如果我们足够接近最终目的地,  就停止

                /* If we are close enough to the final destination then either stop moving or reverse if
                 * the character is set to loop on paths */
                if (Vector2.Distance(transform.position, finalDestination) < stopRadius)
                {
                    targetPosition = finalDestination;

                    rb.velocity = Vector2.zero;
                    return(Vector2.zero);
                }
            }

            /* 在给定路径上,得到最接近的位置点的参数*/
            float param = path.getParam(transform.position);

            /* 向下移动的路径 */
            param += pathDirection * pathOffset;

            /* 得到目标位置 */
            targetPosition = path.getPosition(param, pathLoop);
        }

        return(steeringBasics.Arrive(targetPosition));
    }