public void Update()
        {
            if (Time.time >= nextRepath && canSearchAgain)
            {
                RecalculatePath();
            }

            Vector3 pos = transform.position;

            if (vectorPath != null && vectorPath.Count != 0)
            {
                while ((controller.To2D(pos - vectorPath[wp]).sqrMagnitude < moveNextDist * moveNextDist && wp != vectorPath.Count - 1) || wp == 0)
                {
                    wp++;
                }

                // Current path segment goes from vectorPath[wp-1] to vectorPath[wp]
                // We want to find the point on that segment that is 'moveNextDist' from our current position.
                // This can be visualized as finding the intersection of a circle with radius 'moveNextDist'
                // centered at our current position with that segment.
                var p1 = vectorPath[wp - 1];
                var p2 = vectorPath[wp];

                // Calculate the intersection with the circle. This involves some math.
                var t = VectorMath.LineCircleIntersectionFactor(controller.To2D(transform.position), controller.To2D(p1), controller.To2D(p2), moveNextDist);
                // Clamp to a point on the segment
                t = Mathf.Clamp01(t);
                Vector3 waypoint = Vector3.Lerp(p1, p2, t);

                // Calculate distance to the end of the path
                float remainingDistance = controller.To2D(waypoint - pos).magnitude + controller.To2D(waypoint - p2).magnitude;
                for (int i = wp; i < vectorPath.Count - 1; i++)
                {
                    remainingDistance += controller.To2D(vectorPath[i + 1] - vectorPath[i]).magnitude;
                }

                // Set the target to a point in the direction of the current waypoint at a distance
                // equal to the remaining distance along the path. Since the rvo agent assumes that
                // it should stop when it reaches the target point, this will produce good avoidance
                // behavior near the end of the path. When not close to the end point it will act just
                // as being commanded to move in a particular direction, not toward a particular point
                var rvoTarget = (waypoint - pos).normalized * remainingDistance + pos;
                // When within [slowdownDistance] units from the target, use a progressively lower speed
                var desiredSpeed = Mathf.Clamp01(remainingDistance / slowdownDistance) * maxSpeed;
                Debug.DrawLine(transform.position, waypoint, Color.red);
                controller.SetTarget(rvoTarget, desiredSpeed, maxSpeed);
            }
            else
            {
                // Stand still
                controller.SetTarget(pos, maxSpeed, maxSpeed);
            }

            // Get a processed movement delta from the rvo controller and move the character.
            // This is based on information from earlier frames.
            var movementDelta = controller.CalculateMovementDelta(Time.deltaTime);

            pos += movementDelta;

            // Rotate the character if the velocity is not extremely small
            if (Time.deltaTime > 0 && movementDelta.magnitude / Time.deltaTime > 0.01f)
            {
                var         rot           = transform.rotation;
                var         targetRot     = Quaternion.LookRotation(movementDelta, controller.To3D(Vector2.zero, 1));
                const float RotationSpeed = 5;
                if (controller.movementPlane == MovementPlane.XY)
                {
                    targetRot = targetRot * Quaternion.Euler(-90, 180, 0);
                }
                transform.rotation = Quaternion.Slerp(rot, targetRot, Time.deltaTime * RotationSpeed);
            }

            if (controller.movementPlane == MovementPlane.XZ)
            {
                RaycastHit hit;
                if (Physics.Raycast(pos + Vector3.up, Vector3.down, out hit, 2, groundMask))
                {
                    pos.y = hit.point.y;
                }
            }

            transform.position = pos;
        }
示例#2
0
        public void Update()
        {
            if (Time.time >= nextRepath && canSearchAgain)
            {
                RecalculatePath();
            }

            Vector3 pos = transform.position;

            if (vectorPath != null && vectorPath.Count != 0)
            {
                //Good Game
                //while ((controller.To2D(pos - vectorPath[wp]).sqrMagnitude < moveNextDist*moveNextDist && wp != vectorPath.Count-1) || wp == 0)
                //while ((controller.To2D((VInt3)pos - vectorPath[wp]).sqrMagnitude < moveNextDist*moveNextDist && wp != vectorPath.Count-1) || wp == 0)
                //Debug.Log($"--waypoint--{gameObject.name}--{(controller.To2D((VInt3)pos - vectorPath[wp])).sqrMagnitude / 1000000f}--{moveNextDist * moveNextDist}");
                while ((((Vector2)controller.To2D((VInt3)pos - vectorPath[wp])).sqrMagnitude < moveNextDist * moveNextDist && wp != vectorPath.Count - 1) || wp == 0)
                {
                    wp++;
                    //Debug.Log($"--agent{transform.GetSiblingIndex()}--wp--{wp}");
                }

                // Current path segment goes from vectorPath[wp-1] to vectorPath[wp]
                // We want to find the point on that segment that is 'moveNextDist' from our current position.
                // This can be visualized as finding the intersection of a circle with radius 'moveNextDist'
                // centered at our current position with that segment.
                var p1 = vectorPath[wp - 1];
                var p2 = vectorPath[wp];

                // Calculate the intersection with the circle. This involves some math.
                //Good Game
                //var t = VectorMath.LineCircleIntersectionFactor(controller.To2D(transform.position), controller.To2D(p1), controller.To2D(p2), moveNextDist);
                var t = VectorMath.LineCircleIntersectionFactor((Vector2)controller.To2D((VInt3)transform.position), (Vector2)controller.To2D(p1), (Vector2)controller.To2D(p2), moveNextDist);
                // Clamp to a point on the segment
                t = Mathf.Clamp01(t);
                //Good Game
                //Vector3 waypoint = Vector3.Lerp(p1, p2, t);
                VInt3 waypoint = VInt3.Lerp(p1, p2, t);

                // Calculate distance to the end of the path
                //Good Game

                /*float remainingDistance = controller.To2D(waypoint - pos).magnitude + controller.To2D(waypoint - p2).magnitude;
                 *              for (int i = wp; i < vectorPath.Count - 1; i++)
                 *                  remainingDistance += controller.To2D(vectorPath[i+1] - vectorPath[i]).magnitude;*/
                float remainingDistance = controller.To2D(waypoint - (VInt3)pos).magnitude + controller.To2D(waypoint - p2).magnitude;
                for (int i = wp; i < vectorPath.Count - 1; i++)
                {
                    remainingDistance += controller.To2D(vectorPath[i + 1] - vectorPath[i]).magnitude;
                }

                // Set the target to a point in the direction of the current waypoint at a distance
                // equal to the remaining distance along the path. Since the rvo agent assumes that
                // it should stop when it reaches the target point, this will produce good avoidance
                // behavior near the end of the path. When not close to the end point it will act just
                // as being commanded to move in a particular direction, not toward a particular point
                //GG
                remainingDistance /= 1000000;
                //Debug.Log("--remian--" + remainingDistance.ToString("f2"));
                //var rvoTarget = (waypoint - pos).normalized * remainingDistance + pos;
                var rvoTarget = ((Vector3)waypoint - pos).normalized * remainingDistance + pos;
                // When within [slowdownDistance] units from the target, use a progressively lower speed
                var desiredSpeed = Mathf.Clamp01(remainingDistance / slowdownDistance) * maxSpeed;
                Debug.DrawLine(transform.position, waypoint, Color.red);
                //GG
                //controller.SetTarget(rvoTarget, desiredSpeed, maxSpeed);
                //Debug.Log($"--target--{rvoTarget}--de speed--{desiredSpeed}--maxSpeed--{maxSpeed}");
                controller.SetTarget((VInt3)rvoTarget, (int)(desiredSpeed * 1000), (int)(maxSpeed * 1000));
                //controller.SetTarget((VInt3)rvoTarget, 50, (int)(maxSpeed* 1000));
            }
            else
            {
                // Stand still
                //GG
                //controller.SetTarget(pos, maxSpeed, maxSpeed);
                controller.SetTarget((VInt3)pos, (int)(maxSpeed * 1000), (int)(maxSpeed * 1000));
            }

            // Get a processed movement delta from the rvo controller and move the character.
            // This is based on information from earlier frames.
            //GG
            //var movementDelta = controller.CalculateMovementDelta(Time.deltaTime);
            //var movementDelta = controller.CalculateMovementDelta((int)(Time.deltaTime * 1000));
            var movementDelta = controller.CalculateMovementDelta(50);

            //GG
            //pos += movementDelta;
            pos += (Vector3)movementDelta;
            //Debug.Log("--" + movementDelta.ToString());

            //Rotate the character if the velocity is not extremely small
            //GG
            //if (Time.deltaTime > 0 && movementDelta.magnitude / Time.deltaTime > 0.01f)
            if (Time.deltaTime > 0 && ((Vector3)movementDelta).magnitude / Time.deltaTime > 0.01f)
            {
                var rot = transform.rotation;
                //GG
                //var targetRot = Quaternion.LookRotation((Vector3)movementDelta, (Vector3)controller.To3D(Vector2.zero, 1));
                var         targetRot     = Quaternion.LookRotation((Vector3)movementDelta, (Vector3)controller.To3D(VInt2.zero, 1000));
                const float RotationSpeed = 5;
                if (controller.movementPlane == MovementPlane.XY)
                {
                    targetRot = targetRot * Quaternion.Euler(-90, 180, 0);
                }
                transform.rotation = Quaternion.Slerp(rot, targetRot, Time.deltaTime * RotationSpeed);
            }

            if (controller.movementPlane == MovementPlane.XZ)
            {
                RaycastHit hit;
                if (Physics.Raycast(pos + Vector3.up, Vector3.down, out hit, 2, groundMask))
                {
                    pos.y = hit.point.y;
                }
            }

            transform.position = pos;
            //Good Game

            /*if(pos.x > 200 || pos.y > 200 || pos.z > 200 || pos.x > -200 || pos.z < -200)
             *  Debug.LogError("--" + gameObject.name + "--" + pos);*/
        }