Inheritance: Pathfinding.Util.IAstarPooledObject
示例#1
0
        // Token: 0x0600217B RID: 8571 RVA: 0x0018E3AC File Offset: 0x0018C5AC
        protected override void MovementUpdateInternal(float deltaTime, out Vector3 nextPosition, out Quaternion nextRotation)
        {
            if (this.updatePosition)
            {
                this.simulatedPosition = this.tr.position;
            }
            if (this.updateRotation)
            {
                this.simulatedRotation = this.tr.rotation;
            }
            RichPathPart currentPart = this.richPath.GetCurrentPart();

            if (currentPart is RichSpecial)
            {
                if (!this.traversingOffMeshLink)
                {
                    base.StartCoroutine(this.TraverseSpecial(currentPart as RichSpecial));
                }
                nextPosition = (this.steeringTarget = this.simulatedPosition);
                nextRotation = base.rotation;
                return;
            }
            RichFunnel richFunnel = currentPart as RichFunnel;

            if (richFunnel != null && !base.isStopped)
            {
                this.TraverseFunnel(richFunnel, deltaTime, out nextPosition, out nextRotation);
                return;
            }
            this.velocity2D -= Vector2.ClampMagnitude(this.velocity2D, this.acceleration * deltaTime);
            this.FinalMovement(this.simulatedPosition, deltaTime, float.PositiveInfinity, 1f, out nextPosition, out nextRotation);
            this.steeringTarget = this.simulatedPosition;
        }
示例#2
0
        protected override void MovementUpdate(float deltaTime)
        {
            RichPathPart currentPart = this.richPath.GetCurrentPart();
            RichFunnel   richFunnel  = currentPart as RichFunnel;

            if (richFunnel != null)
            {
                this.TraverseFunnel(richFunnel, deltaTime);
            }
            else if (currentPart is RichSpecial)
            {
                if (!this.traversingSpecialPath)
                {
                    base.StartCoroutine(this.TraverseSpecial(currentPart as RichSpecial));
                }
            }
            else
            {
                if (this.rvoController != null && this.rvoController.enabled)
                {
                    this.rvoController.Move(Vector3.zero);
                }
                base.Move(this.tr.position, Vector3.zero);
            }
            Vector3 position = this.tr.position;

            this.realVelocity = ((deltaTime > 0f) ? ((position - this.prevPosition) / deltaTime) : Vector3.zero);
            this.prevPosition = position;
        }
示例#3
0
        /** Called during either Update or FixedUpdate depending on if rigidbodies are used for movement or not */
        protected override void MovementUpdate(float deltaTime)
        {
            RichPathPart currentPart = richPath.GetCurrentPart();
            var          funnel      = currentPart as RichFunnel;

            if (funnel != null)
            {
                TraverseFunnel(funnel, deltaTime);
            }
            else if (currentPart is RichSpecial)
            {
                // The current path part is a special part, for example a link
                // Movement during this part of the path is handled by the TraverseSpecial coroutine
                if (!traversingSpecialPath)
                {
                    StartCoroutine(TraverseSpecial(currentPart as RichSpecial));
                }
            }
            else
            {
                // Unknown or null path part, just try to stand still
                if (rvoController != null && rvoController.enabled)
                {
                    // Use RVOController
                    rvoController.Move(Vector3.zero);
                }
                Move(tr.position, Vector3.zero);
            }

            var pos = tr.position;

            realVelocity = deltaTime > 0 ? (pos - prevPosition) / deltaTime : Vector3.zero;
            prevPosition = pos;
        }
示例#4
0
        /// <summary>Called during either Update or FixedUpdate depending on if rigidbodies are used for movement or not</summary>
        protected override void MovementUpdateInternal(float deltaTime, out Vector3 nextPosition, out Quaternion nextRotation)
        {
            if (updatePosition)
            {
                simulatedPosition = tr.position;
            }
            if (updateRotation)
            {
                simulatedRotation = tr.rotation;
            }

            RichPathPart currentPart = richPath.GetCurrentPart();

            if (currentPart is RichSpecial)
            {
                // Start traversing the off mesh link if we haven't done it yet
                if (!traversingOffMeshLink && !richPath.CompletedAllParts)
                {
                    StartCoroutine(TraverseSpecial(currentPart as RichSpecial));
                }

                nextPosition = steeringTarget = simulatedPosition;
                nextRotation = rotation;
            }
            else
            {
                var funnel = currentPart as RichFunnel;

                // Check if we have a valid path to follow and some other script has not stopped the character
                bool stopped = isStopped || (reachedDestination && whenCloseToDestination == CloseToDestinationMode.Stop);
                if (rvoController != null)
                {
                    rvoDensityBehavior.Update(reachedDestination, ref stopped, ref rvoController.priorityMultiplier, ref rvoController.flowFollowingStrength);
                }

                if (funnel != null && !stopped)
                {
                    TraverseFunnel(funnel, deltaTime, out nextPosition, out nextRotation);
                }
                else
                {
                    // Unknown, null path part, or the character is stopped
                    // Slow down as quickly as possible
                    velocity2D -= Vector2.ClampMagnitude(velocity2D, acceleration * deltaTime);
                    FinalMovement(simulatedPosition, deltaTime, float.PositiveInfinity, 1f, out nextPosition, out nextRotation);
                    steeringTarget = simulatedPosition;
                }
            }
        }
        /// <summary>Called during either Update or FixedUpdate depending on if rigidbodies are used for movement or not</summary>
        protected override void MovementUpdateInternal(float deltaTime, out Vector3 nextPosition, out Quaternion nextRotation)
        {
            if (updatePosition)
            {
                simulatedPosition = tr.position;
            }
            if (updateRotation)
            {
                simulatedRotation = tr.rotation;
            }

            RichPathPart currentPart = richPath.GetCurrentPart();

            if (currentPart is RichSpecial)
            {
                if (!traversingOffMeshLink)
                {
                    StartCoroutine(TraverseSpecial(currentPart as RichSpecial));
                }

                nextPosition = steeringTarget = simulatedPosition;
                nextRotation = rotation;
            }
            else
            {
                var funnel = currentPart as RichFunnel;
                if (funnel != null && !isStopped)
                {
                    TraverseFunnel(funnel, deltaTime, out nextPosition, out nextRotation);
                }
                else
                {
                    // Unknown, null path part, or the character is stopped
                    // Slow down as quickly as possible
                    velocity2D -= Vector2.ClampMagnitude(velocity2D, acceleration * deltaTime);
                    FinalMovement(simulatedPosition, deltaTime, float.PositiveInfinity, 1f, out nextPosition, out nextRotation);
                    steeringTarget = simulatedPosition;
                }
            }
        }
示例#6
0
        /** Update is called once per frame */
        protected virtual void Update()
        {
            deltaTime = Mathf.Min(Time.smoothDeltaTime * 2, Time.deltaTime);

            if (rp != null)
            {
                //System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch();
                //w.Start();
                RichPathPart pt = rp.GetCurrentPart();
                var          fn = pt as RichFunnel;
                if (fn != null)
                {
                    //Clear buffers for reuse
                    Vector3 position = UpdateTarget(fn);

                    //tr.position = ps;

                    //Only get walls every 5th frame to save on performance
                    if (Time.frameCount % 5 == 0 && wallForce > 0 && wallDist > 0)
                    {
                        wallBuffer.Clear();
                        fn.FindWalls(wallBuffer, wallDist);
                    }

                    /*for (int i=0;i<wallBuffer.Count;i+=2) {
                     *  Debug.DrawLine (wallBuffer[i],wallBuffer[i+1],Color.magenta);
                     * }*/

                    //Pick next waypoint if current is reached
                    int tgIndex = 0;

                    /*if (buffer.Count > 1) {
                     *  if ((buffer[tgIndex]-tr.position).sqrMagnitude < pickNextWaypointDist*pickNextWaypointDist) {
                     *      tgIndex++;
                     *  }
                     * }*/


                    //Target point
                    Vector3 tg  = buffer[tgIndex];
                    Vector3 dir = tg - position;
                    dir.y = 0;

                    bool passedTarget = Vector3.Dot(dir, currentTargetDirection) < 0;
                    //Check if passed target in another way
                    if (passedTarget && buffer.Count - tgIndex > 1)
                    {
                        tgIndex++;
                        tg = buffer[tgIndex];
                    }

                    if (tg != lastTargetPoint)
                    {
                        currentTargetDirection   = (tg - position);
                        currentTargetDirection.y = 0;
                        currentTargetDirection.Normalize();
                        lastTargetPoint = tg;
                        //Debug.DrawRay (tr.position, Vector3.down*2,Color.blue,0.2f);
                    }

                    //Direction to target
                    dir   = (tg - position);
                    dir.y = 0;
                    float magn = dir.magnitude;

                    //Write out for other scripts to read
                    distanceToWaypoint = magn;

                    //Normalize
                    dir = magn == 0 ? Vector3.zero : dir / magn;
                    Vector3 normdir = dir;

                    Vector3 force = Vector3.zero;

                    if (wallForce > 0 && wallDist > 0)
                    {
                        float wLeft  = 0;
                        float wRight = 0;

                        for (int i = 0; i < wallBuffer.Count; i += 2)
                        {
                            Vector3 closest = VectorMath.ClosestPointOnSegment(wallBuffer[i], wallBuffer[i + 1], tr.position);
                            float   dist    = (closest - position).sqrMagnitude;

                            if (dist > wallDist * wallDist)
                            {
                                continue;
                            }

                            Vector3 tang = (wallBuffer[i + 1] - wallBuffer[i]).normalized;

                            //Using the fact that all walls are laid out clockwise (seeing from inside)
                            //Then left and right (ish) can be figured out like this
                            float dot = Vector3.Dot(dir, tang) * (1 - System.Math.Max(0, (2 * (dist / (wallDist * wallDist)) - 1)));
                            if (dot > 0)
                            {
                                wRight = System.Math.Max(wRight, dot);
                            }
                            else
                            {
                                wLeft = System.Math.Max(wLeft, -dot);
                            }
                        }

                        Vector3 norm = Vector3.Cross(Vector3.up, dir);
                        force = norm * (wRight - wLeft);

                        //Debug.DrawRay (tr.position, force, Color.cyan);
                    }

                    //Is the endpoint of the path (part) the current target point
                    bool endPointIsTarget = lastCorner && buffer.Count - tgIndex == 1;

                    if (endPointIsTarget)
                    {
                        //Use 2nd or 3rd degree motion equation to figure out acceleration to reach target in "exact" [slowdownTime] seconds

                        //Clamp to avoid divide by zero
                        if (slowdownTime < 0.001f)
                        {
                            slowdownTime = 0.001f;
                        }

                        Vector3 diff = tg - position;
                        diff.y = 0;

                        if (preciseSlowdown)
                        {
                            //{ t = slowdownTime
                            //{ diff = vt + at^2/2 + qt^3/6
                            //{ 0 = at + qt^2/2
                            //{ solve for a
                            dir = (6 * diff - 4 * slowdownTime * velocity) / (slowdownTime * slowdownTime);
                        }
                        else
                        {
                            dir = 2 * (diff - slowdownTime * velocity) / (slowdownTime * slowdownTime);
                        }
                        dir = Vector3.ClampMagnitude(dir, acceleration);

                        force *= System.Math.Min(magn / 0.5f, 1);

                        if (magn < endReachedDistance)
                        {
                            //END REACHED
                            NextPart();
                        }
                    }
                    else
                    {
                        dir *= acceleration;
                    }

                    //Debug.DrawRay (tr.position+Vector3.up, dir*3, Color.blue);

                    velocity += (dir + force * wallForce) * deltaTime;

                    if (slowWhenNotFacingTarget)
                    {
                        float dot = (Vector3.Dot(normdir, tr.forward) + 0.5f) * (1.0f / 1.5f);
                        //velocity = Vector3.ClampMagnitude (velocity, maxSpeed * Mathf.Max (dot, 0.2f) );
                        float xzmagn = Mathf.Sqrt(velocity.x * velocity.x + velocity.z * velocity.z);
                        float prevy  = velocity.y;
                        velocity.y = 0;
                        float mg = Mathf.Min(xzmagn, maxSpeed * Mathf.Max(dot, 0.2f));
                        velocity = Vector3.Lerp(tr.forward * mg, velocity.normalized * mg, Mathf.Clamp(endPointIsTarget ? (magn * 2) : 0, 0.5f, 1.0f));

                        velocity.y = prevy;
                    }
                    else
                    {
                        // Clamp magnitude on the XZ axes
                        float xzmagn = Mathf.Sqrt(velocity.x * velocity.x + velocity.z * velocity.z);
                        xzmagn = maxSpeed / xzmagn;
                        if (xzmagn < 1)
                        {
                            velocity.x *= xzmagn;
                            velocity.z *= xzmagn;
                            //Vector3.ClampMagnitude (velocity, maxSpeed);
                        }
                    }

                    //Debug.DrawLine (tr.position, tg, lastCorner ? Color.red : Color.green);


                    if (endPointIsTarget)
                    {
                        Vector3 trotdir = Vector3.Lerp(velocity, currentTargetDirection, System.Math.Max(1 - magn * 2, 0));
                        RotateTowards(trotdir);
                    }
                    else
                    {
                        RotateTowards(velocity);
                    }

                    //Applied after rotation to enable proper checks on if velocity is zero
                    velocity += deltaTime * gravity;

                    if (rvoController != null && rvoController.enabled)
                    {
                        //Use RVOController
                        tr.position = position;
                        rvoController.Move(velocity);
                    }
                    else
                    if (controller != null && controller.enabled)
                    {
                        //Use CharacterController
                        tr.position = position;
                        controller.Move(velocity * deltaTime);
                    }
                    else
                    {
                        //Use Transform
                        float lasty = position.y;
                        position += velocity * deltaTime;

                        position = RaycastPosition(position, lasty);

                        tr.position = position;
                    }
                }
                else
                {
                    if (rvoController != null && rvoController.enabled)
                    {
                        //Use RVOController
                        rvoController.Move(Vector3.zero);
                    }
                }

                if (pt is RichSpecial)
                {
                    if (!traversingSpecialPath)
                    {
                        StartCoroutine(TraverseSpecial(pt as RichSpecial));
                    }
                }
                //w.Stop();
                //Debug.Log ((w.Elapsed.TotalMilliseconds*1000));
            }
            else
            {
                if (rvoController != null && rvoController.enabled)
                {
                    //Use RVOController
                    rvoController.Move(Vector3.zero);
                }
                else
                if (controller != null && controller.enabled)
                {
                }
                else
                {
                    tr.position = RaycastPosition(tr.position, tr.position.y);
                }
            }
        }
示例#7
0
 protected virtual void Update()
 {
     RichAI.deltaTime = Mathf.Min(Time.smoothDeltaTime * 2f, Time.deltaTime);
     if (this.rp != null)
     {
         RichPathPart currentPart = this.rp.GetCurrentPart();
         RichFunnel   richFunnel  = currentPart as RichFunnel;
         if (richFunnel != null)
         {
             Vector3 vector = this.UpdateTarget(richFunnel);
             if (Time.frameCount % 5 == 0)
             {
                 this.wallBuffer.Clear();
                 richFunnel.FindWalls(this.wallBuffer, this.wallDist);
             }
             int     num     = 0;
             Vector3 vector2 = this.buffer[num];
             Vector3 vector3 = vector2 - vector;
             vector3.y = 0f;
             bool flag = Vector3.Dot(vector3, this.currentTargetDirection) < 0f;
             if (flag && this.buffer.Count - num > 1)
             {
                 num++;
                 vector2 = this.buffer[num];
             }
             if (vector2 != this.lastTargetPoint)
             {
                 this.currentTargetDirection   = vector2 - vector;
                 this.currentTargetDirection.y = 0f;
                 this.currentTargetDirection.Normalize();
                 this.lastTargetPoint = vector2;
             }
             vector3   = vector2 - vector;
             vector3.y = 0f;
             float magnitude = vector3.magnitude;
             this.distanceToWaypoint = magnitude;
             vector3 = ((magnitude != 0f) ? (vector3 / magnitude) : Vector3.zero);
             Vector3 vector4 = vector3;
             Vector3 vector5 = Vector3.zero;
             if (this.wallForce > 0f && this.wallDist > 0f)
             {
                 float num2 = 0f;
                 float num3 = 0f;
                 for (int i = 0; i < this.wallBuffer.Count; i += 2)
                 {
                     Vector3 vector6      = AstarMath.NearestPointStrict(this.wallBuffer[i], this.wallBuffer[i + 1], this.tr.position);
                     float   sqrMagnitude = (vector6 - vector).sqrMagnitude;
                     if (sqrMagnitude <= this.wallDist * this.wallDist)
                     {
                         Vector3 normalized = (this.wallBuffer[i + 1] - this.wallBuffer[i]).normalized;
                         float   num4       = Vector3.Dot(vector3, normalized) * (1f - Math.Max(0f, 2f * (sqrMagnitude / (this.wallDist * this.wallDist)) - 1f));
                         if (num4 > 0f)
                         {
                             num3 = Math.Max(num3, num4);
                         }
                         else
                         {
                             num2 = Math.Max(num2, -num4);
                         }
                     }
                 }
                 Vector3 vector7 = Vector3.Cross(Vector3.up, vector3);
                 vector5 = vector7 * (num3 - num2);
             }
             bool flag2 = this.lastCorner && this.buffer.Count - num == 1;
             if (flag2)
             {
                 if (this.slowdownTime < 0.001f)
                 {
                     this.slowdownTime = 0.001f;
                 }
                 Vector3 vector8 = vector2 - vector;
                 vector8.y = 0f;
                 if (this.preciseSlowdown)
                 {
                     vector3 = (6f * vector8 - 4f * this.slowdownTime * this.velocity) / (this.slowdownTime * this.slowdownTime);
                 }
                 else
                 {
                     vector3 = 2f * (vector8 - this.slowdownTime * this.velocity) / (this.slowdownTime * this.slowdownTime);
                 }
                 vector3  = Vector3.ClampMagnitude(vector3, this.acceleration);
                 vector5 *= Math.Min(magnitude / 0.5f, 1f);
                 if (magnitude < this.endReachedDistance)
                 {
                     this.NextPart();
                 }
             }
             else
             {
                 vector3 *= this.acceleration;
             }
             this.velocity += (vector3 + vector5 * this.wallForce) * RichAI.deltaTime;
             if (this.slowWhenNotFacingTarget)
             {
                 float num5 = (Vector3.Dot(vector4, this.tr.forward) + 0.5f) * 0.6666667f;
                 float num6 = Mathf.Sqrt(this.velocity.x * this.velocity.x + this.velocity.z * this.velocity.z);
                 float y    = this.velocity.y;
                 this.velocity.y = 0f;
                 float num7 = Mathf.Min(num6, this.maxSpeed * Mathf.Max(num5, 0.2f));
                 this.velocity   = Vector3.Lerp(this.tr.forward * num7, this.velocity.normalized * num7, Mathf.Clamp((!flag2) ? 0f : (magnitude * 2f), 0.5f, 1f));
                 this.velocity.y = y;
             }
             else
             {
                 float num8 = Mathf.Sqrt(this.velocity.x * this.velocity.x + this.velocity.z * this.velocity.z);
                 num8 = this.maxSpeed / num8;
                 if (num8 < 1f)
                 {
                     this.velocity.x = this.velocity.x * num8;
                     this.velocity.z = this.velocity.z * num8;
                 }
             }
             if (flag2)
             {
                 Vector3 trotdir = Vector3.Lerp(this.velocity, this.currentTargetDirection, Math.Max(1f - magnitude * 2f, 0f));
                 this.RotateTowards(trotdir);
             }
             else
             {
                 this.RotateTowards(this.velocity);
             }
             this.velocity += RichAI.deltaTime * this.gravity;
             if (this.rvoController != null && this.rvoController.enabled)
             {
                 this.tr.position = vector;
                 this.rvoController.Move(this.velocity);
             }
             else if (this.controller != null && this.controller.enabled)
             {
                 this.tr.position = vector;
                 this.controller.Move(this.velocity * RichAI.deltaTime);
             }
             else
             {
                 float y2 = vector.y;
                 vector          += this.velocity * RichAI.deltaTime;
                 vector           = this.RaycastPosition(vector, y2);
                 this.tr.position = vector;
             }
         }
         else if (this.rvoController != null && this.rvoController.enabled)
         {
             this.rvoController.Move(Vector3.zero);
         }
         if (currentPart is RichSpecial)
         {
             RichSpecial rs = currentPart as RichSpecial;
             if (!this.traversingSpecialPath)
             {
                 base.StartCoroutine(this.TraverseSpecial(rs));
             }
         }
     }
     else if (this.rvoController != null && this.rvoController.enabled)
     {
         this.rvoController.Move(Vector3.zero);
     }
     else if (!(this.controller != null) || !this.controller.enabled)
     {
         this.tr.position = this.RaycastPosition(this.tr.position, this.tr.position.y);
     }
 }
示例#8
0
文件: RichAI.cs 项目: K07H/The-Forest
 protected virtual void Update()
 {
     RichAI.deltaTime = Mathf.Min(Time.smoothDeltaTime * 2f, Time.deltaTime);
     if (this.rp != null)
     {
         RichPathPart currentPart = this.rp.GetCurrentPart();
         RichFunnel   richFunnel  = currentPart as RichFunnel;
         if (richFunnel != null)
         {
             Vector3 vector = this.UpdateTarget(richFunnel);
             if (Time.frameCount % 5 == 0 && this.wallForce > 0f && this.wallDist > 0f)
             {
                 this.wallBuffer.Clear();
                 richFunnel.FindWalls(this.wallBuffer, this.wallDist);
             }
             int     num     = 0;
             Vector3 vector2 = this.nextCorners[num];
             Vector3 vector3 = vector2 - vector;
             vector3.y = 0f;
             bool flag = Vector3.Dot(vector3, this.currentTargetDirection) < 0f;
             if (flag && this.nextCorners.Count - num > 1)
             {
                 num++;
                 vector2 = this.nextCorners[num];
             }
             if (vector2 != this.lastTargetPoint)
             {
                 this.currentTargetDirection   = vector2 - vector;
                 this.currentTargetDirection.y = 0f;
                 this.currentTargetDirection.Normalize();
                 this.lastTargetPoint = vector2;
             }
             vector3   = vector2 - vector;
             vector3.y = 0f;
             Vector3 vector4 = VectorMath.Normalize(vector3, out this.distanceToWaypoint);
             bool    flag2   = this.lastCorner && this.nextCorners.Count - num == 1;
             if (flag2 && this.distanceToWaypoint < 0.01f * this.maxSpeed)
             {
                 this.velocity = (vector2 - vector) * 100f;
             }
             else
             {
                 Vector3 a = this.CalculateWallForce(vector, vector4);
                 Vector2 vector5;
                 if (flag2)
                 {
                     vector5 = this.CalculateAccelerationToReachPoint(RichAI.To2D(vector2 - vector), Vector2.zero, RichAI.To2D(this.velocity));
                     a      *= Math.Min(this.distanceToWaypoint / 0.5f, 1f);
                     if (this.distanceToWaypoint < this.endReachedDistance)
                     {
                         this.NextPart();
                     }
                 }
                 else
                 {
                     Vector3 a2 = (num >= this.nextCorners.Count - 1) ? ((vector2 - vector) * 2f + vector) : this.nextCorners[num + 1];
                     Vector3 v  = (a2 - vector2).normalized * this.maxSpeed;
                     vector5 = this.CalculateAccelerationToReachPoint(RichAI.To2D(vector2 - vector), RichAI.To2D(v), RichAI.To2D(this.velocity));
                 }
                 this.velocity += (new Vector3(vector5.x, 0f, vector5.y) + a * this.wallForce) * RichAI.deltaTime;
             }
             TriangleMeshNode currentNode = richFunnel.CurrentNode;
             Vector3          b;
             if (currentNode != null)
             {
                 b = currentNode.ClosestPointOnNode(vector);
             }
             else
             {
                 b = vector;
             }
             float magnitude = (richFunnel.exactEnd - b).magnitude;
             float num2      = this.maxSpeed;
             num2 *= Mathf.Sqrt(Mathf.Min(1f, magnitude / (this.maxSpeed * this.slowdownTime)));
             if (this.slowWhenNotFacingTarget)
             {
                 float num3 = Mathf.Max((Vector3.Dot(vector4, this.tr.forward) + 0.5f) / 1.5f, 0.2f);
                 num2 *= num3;
                 float num4 = VectorMath.MagnitudeXZ(this.velocity);
                 float y    = this.velocity.y;
                 this.velocity.y = 0f;
                 num4            = Mathf.Min(num4, num2);
                 this.velocity   = Vector3.Lerp(this.velocity.normalized * num4, this.tr.forward * num4, Mathf.Clamp((!flag2) ? 1f : (this.distanceToWaypoint * 2f), 0f, 0.5f));
                 this.velocity.y = y;
             }
             else
             {
                 this.velocity = VectorMath.ClampMagnitudeXZ(this.velocity, num2);
             }
             this.velocity += RichAI.deltaTime * this.gravity;
             if (this.rvoController != null && this.rvoController.enabled)
             {
                 Vector3 pos = vector + VectorMath.ClampMagnitudeXZ(this.velocity, magnitude);
                 this.rvoController.SetTarget(pos, VectorMath.MagnitudeXZ(this.velocity), this.maxSpeed);
             }
             Vector3 vector6;
             if (this.rvoController != null && this.rvoController.enabled)
             {
                 vector6   = this.rvoController.CalculateMovementDelta(vector, RichAI.deltaTime);
                 vector6.y = this.velocity.y * RichAI.deltaTime;
             }
             else
             {
                 vector6 = this.velocity * RichAI.deltaTime;
             }
             if (flag2)
             {
                 Vector3 trotdir = Vector3.Lerp(vector6.normalized, this.currentTargetDirection, Math.Max(1f - this.distanceToWaypoint * 2f, 0f));
                 this.RotateTowards(trotdir);
             }
             else
             {
                 this.RotateTowards(vector6);
             }
             if (this.controller != null && this.controller.enabled)
             {
                 this.tr.position = vector;
                 this.controller.Move(vector6);
                 vector = this.tr.position;
             }
             else
             {
                 float y2 = vector.y;
                 vector += vector6;
                 vector  = this.RaycastPosition(vector, y2);
             }
             Vector3 vector7 = richFunnel.ClampToNavmesh(vector);
             if (vector != vector7)
             {
                 Vector3 vector8 = vector7 - vector;
                 this.velocity -= vector8 * Vector3.Dot(vector8, this.velocity) / vector8.sqrMagnitude;
                 if (this.rvoController != null && this.rvoController.enabled)
                 {
                     this.rvoController.SetCollisionNormal(vector8);
                 }
             }
             this.tr.position = vector7;
         }
         else if (this.rvoController != null && this.rvoController.enabled)
         {
             this.rvoController.Move(Vector3.zero);
         }
         if (currentPart is RichSpecial && !this.traversingSpecialPath)
         {
             base.StartCoroutine(this.TraverseSpecial(currentPart as RichSpecial));
         }
     }
     else if (this.rvoController != null && this.rvoController.enabled)
     {
         this.rvoController.Move(Vector3.zero);
     }
     else if (!(this.controller != null) || !this.controller.enabled)
     {
         this.tr.position = this.RaycastPosition(this.tr.position, this.tr.position.y);
     }
 }
示例#9
0
        /** Update is called once per frame */
        protected virtual void Update()
        {
            deltaTime = Mathf.Min(Time.smoothDeltaTime * 2, Time.deltaTime);

            if (rp != null)
            {
                RichPathPart currentPart = rp.GetCurrentPart();
                var          fn          = currentPart as RichFunnel;
                if (fn != null)
                {
                    // Clamp the current position to the navmesh
                    // and update the list of upcoming corners in the path
                    // and store that in the 'nextCorners' variable
                    Vector3 position = UpdateTarget(fn);

                    // Only get walls every 5th frame to save on performance
                    if (Time.frameCount % 5 == 0 && wallForce > 0 && wallDist > 0)
                    {
                        wallBuffer.Clear();
                        fn.FindWalls(wallBuffer, wallDist);
                    }

                    // Target point
                    int     tgIndex     = 0;
                    Vector3 targetPoint = nextCorners[tgIndex];
                    Vector3 dir         = targetPoint - position;
                    dir.y = 0;

                    bool passedTarget = Vector3.Dot(dir, currentTargetDirection) < 0;
                    // Check if passed target in another way
                    if (passedTarget && nextCorners.Count - tgIndex > 1)
                    {
                        tgIndex++;
                        targetPoint = nextCorners[tgIndex];
                    }

                    // Check if the target point changed compared to last frame
                    if (targetPoint != lastTargetPoint)
                    {
                        currentTargetDirection   = targetPoint - position;
                        currentTargetDirection.y = 0;
                        currentTargetDirection.Normalize();
                        lastTargetPoint = targetPoint;
                    }

                    // Direction to target
                    dir   = targetPoint - position;
                    dir.y = 0;

                    // Normalized direction
                    Vector3 normdir = VectorMath.Normalize(dir, out distanceToWaypoint);

                    // Is the endpoint of the path (part) the current target point
                    bool targetIsEndPoint = lastCorner && nextCorners.Count - tgIndex == 1;

                    // When very close to the target point, move directly towards the target
                    // instead of using accelerations as they tend to be a bit jittery in this case
                    if (targetIsEndPoint && distanceToWaypoint < 0.01f * maxSpeed)
                    {
                        // Velocity will be at most 1 times max speed, it will be further clamped below
                        velocity = (targetPoint - position) * 100;
                    }
                    else
                    {
                        // Calculate force from walls
                        Vector3 wallForceVector = CalculateWallForce(position, normdir);
                        Vector2 accelerationVector;

                        if (targetIsEndPoint)
                        {
                            accelerationVector = CalculateAccelerationToReachPoint(To2D(targetPoint - position), Vector2.zero, To2D(velocity));
                            //accelerationVector = Vector3.ClampMagnitude(accelerationVector, acceleration);

                            // Reduce the wall avoidance force as we get closer to our target
                            wallForceVector *= System.Math.Min(distanceToWaypoint / 0.5f, 1);

                            if (distanceToWaypoint < endReachedDistance)
                            {
                                // END REACHED
                                NextPart();
                            }
                        }
                        else
                        {
                            var nextNextCorner = tgIndex < nextCorners.Count - 1 ? nextCorners[tgIndex + 1] : (targetPoint - position) * 2 + position;
                            var targetVelocity = (nextNextCorner - targetPoint).normalized * maxSpeed;

                            accelerationVector = CalculateAccelerationToReachPoint(To2D(targetPoint - position), To2D(targetVelocity), To2D(velocity));
                        }

                        // Update the velocity using the acceleration
                        velocity += (new Vector3(accelerationVector.x, 0, accelerationVector.y) + wallForceVector * wallForce) * deltaTime;
                    }

                    var currentNode = fn.CurrentNode;

                    Vector3 closestOnNode;
                    if (currentNode != null)
                    {
                        closestOnNode = currentNode.ClosestPointOnNode(position);
                    }
                    else
                    {
                        closestOnNode = position;
                    }

                    // Distance to the end of the path (as the crow flies)
                    var distToEndOfPath = (fn.exactEnd - closestOnNode).magnitude;

                    // Max speed to use for this frame
                    var currentMaxSpeed = maxSpeed;
                    currentMaxSpeed *= Mathf.Sqrt(Mathf.Min(1, distToEndOfPath / (maxSpeed * slowdownTime)));

                    // Check if the agent should slow down in case it is not facing the direction it wants to move in
                    if (slowWhenNotFacingTarget)
                    {
                        // 1 when normdir is in the same direction as tr.forward
                        // 0.2 when they point in the opposite directions
                        float directionSpeedFactor = Mathf.Max((Vector3.Dot(normdir, tr.forward) + 0.5f) / 1.5f, 0.2f);
                        currentMaxSpeed *= directionSpeedFactor;
                        float currentSpeed = VectorMath.MagnitudeXZ(velocity);
                        float prevy        = velocity.y;
                        velocity.y   = 0;
                        currentSpeed = Mathf.Min(currentSpeed, currentMaxSpeed);

                        // Make sure the agent always moves in the forward direction
                        // except when getting close to the end of the path in which case
                        // the velocity can be in any direction
                        velocity = Vector3.Lerp(velocity.normalized * currentSpeed, tr.forward * currentSpeed, Mathf.Clamp(targetIsEndPoint ? distanceToWaypoint * 2 : 1, 0.0f, 0.5f));

                        velocity.y = prevy;
                    }
                    else
                    {
                        velocity = VectorMath.ClampMagnitudeXZ(velocity, currentMaxSpeed);
                    }

                    // Apply gravity
                    velocity += deltaTime * gravity;

                    if (rvoController != null && rvoController.enabled)
                    {
                        // Send a message to the RVOController that we want to move
                        // with this velocity. In the next simulation step, this velocity
                        // will be processed and it will be fed back the rvo controller
                        // and finally it will be used by this script when calling the
                        // CalculateMovementDelta method below

                        // Make sure that we don't move further than to the end point of the path
                        // If the RVO simulation FPS is low and we did not do this, the agent
                        // might overshoot the target a lot.
                        var rvoTarget = position + VectorMath.ClampMagnitudeXZ(velocity, distToEndOfPath);
                        rvoController.SetTarget(rvoTarget, VectorMath.MagnitudeXZ(velocity), maxSpeed);
                    }

                    // Direction and distance to move during this frame
                    Vector3 deltaPosition;
                    if (rvoController != null && rvoController.enabled)
                    {
                        // Use RVOController to get a processed delta position
                        // such that collisions will be avoided if possible
                        deltaPosition = rvoController.CalculateMovementDelta(position, deltaTime);

                        // The RVOController does not know about gravity
                        // so we copy it from the normal velocity calculation
                        deltaPosition.y = velocity.y * deltaTime;
                    }
                    else
                    {
                        deltaPosition = velocity * deltaTime;
                    }

                    if (targetIsEndPoint)
                    {
                        // Rotate towards the direction that the agent was in
                        // when the target point was seen for the first time
                        // TODO: Some magic constants here, should probably compute them from other variables
                        // or expose them as separate variables
                        Vector3 trotdir = Vector3.Lerp(deltaPosition.normalized, currentTargetDirection, System.Math.Max(1 - distanceToWaypoint * 2, 0));
                        RotateTowards(trotdir);
                    }
                    else
                    {
                        // Rotate towards the direction we are moving in
                        RotateTowards(deltaPosition);
                    }

                    if (controller != null && controller.enabled)
                    {
                        // Use CharacterController
                        tr.position = position;
                        controller.Move(deltaPosition);
                        // Grab the position after the movement to be able to take physics into account
                        position = tr.position;
                    }
                    else
                    {
                        // Use Transform
                        float lastY = position.y;
                        position += deltaPosition;
                        // Position the character on the ground
                        position = RaycastPosition(position, lastY);
                    }

                    // Clamp the position to the navmesh after movement is done
                    var clampedPosition = fn.ClampToNavmesh(position);

                    if (position != clampedPosition)
                    {
                        // The agent was outside the navmesh. Remove that component of the velocity
                        // so that the velocity only goes along the direction of the wall, not into it
                        var difference = clampedPosition - position;
                        velocity -= difference * Vector3.Dot(difference, velocity) / difference.sqrMagnitude;

                        // Make sure the RVO system knows that there was a collision here
                        // Otherwise other agents may think this agent continued to move forwards
                        // and avoidance quality may suffer
                        if (rvoController != null && rvoController.enabled)
                        {
                            rvoController.SetCollisionNormal(difference);
                        }
                    }

                    tr.position = clampedPosition;
                }
                else
                {
                    if (rvoController != null && rvoController.enabled)
                    {
                        //Use RVOController
                        rvoController.Move(Vector3.zero);
                    }
                }
                if (currentPart is RichSpecial)
                {
                    // The current path part is a special part, for example a link
                    // Movement during this part of the path is handled by the TraverseSpecial coroutine
                    if (!traversingSpecialPath)
                    {
                        StartCoroutine(TraverseSpecial(currentPart as RichSpecial));
                    }
                }
            }
            else
            {
                if (rvoController != null && rvoController.enabled)
                {
                    // Use RVOController
                    rvoController.Move(Vector3.zero);
                }
                else
                if (controller != null && controller.enabled)
                {
                }
                else
                {
                    tr.position = RaycastPosition(tr.position, tr.position.y);
                }
            }
        }