예제 #1
0
        void Update()
        {
            // If there is no target set, there is no need for calculations.
            if (!controller.Target)
            {
                return;
            }

            elapsedTime += Time.deltaTime;

            // Only use guidance when the life time of the missile is greater than the set delay.
            if (elapsedTime < delay)
            {
                return;
            }


            // Do some fancy math which is too complicated for a little comment. I suggest reading the Wikipedia entry on proportional navigation if you want to know how it works.
            previousLos = los;
            los         = controller.Target.position - transform.position;
            losDelta    = los - previousLos;

            losDelta = losDelta - Vector3.Project(losDelta, los);

            if (advancedMode)
            {
                desiredRotation = (Time.deltaTime * los) + (losDelta * navigationalConstant) + (Time.deltaTime * desiredRotation * navigationalConstant * 0.5f); // Augmented version of proportional navigation.
            }
            else
            {
                desiredRotation = (Time.deltaTime * los) + (losDelta * navigationalConstant); // Plain proportional navigation.
            }
            controller.Rotate(Quaternion.LookRotation(desiredRotation, transform.up));        // Use the Rotate function of the controller, instead of the transforms, to consider the rotation rate of the missile.
        }
        void Update()
        {
            // If there is no target set, there is no need for calculations.
            if (!controller.Target)
            {
                return;
            }

            elapsedTime += Time.deltaTime;

            // Only use guidance when the life time of the missile is greater than the set delay.
            if (elapsedTime < delay)
            {
                return;
            }

            if (advancedMode)
            {
                // The advanced mode only works when the missile is closing in on the target, so we need to make sure that it only runs if that's the case.
                // Otherwise we just use the normal lookat method to rotate.
                currentDistance = Vector3.Distance(transform.position, controller.Target.position);
                if (currentDistance < previousDistance)
                {
                    // This little function reflects the velocity vector along the LoS vector. The resulting vector "aims ahead" to overcome the missiles inertia more quickly.
                    Vector3 velocity         = -controller.Velocity.normalized;
                    Vector3 los              = (controller.Target.position - transform.position).normalized;
                    Vector3 modifiedRotation = Vector3.Reflect(velocity, los);

                    controller.Rotate(Quaternion.LookRotation(modifiedRotation)); // Use the Rotate function of the controller, instead of the transforms, to consider the rotation rate of the missile.
                }
                else
                {
                    controller.Rotate(Quaternion.LookRotation(controller.Target.position - transform.position, transform.up)); // Use the Rotate function of the controller, instead of the transforms, to consider the rotation rate of the missile.
                }

                previousDistance = currentDistance;
            }
            else
            {
                controller.Rotate(Quaternion.LookRotation(controller.Target.position - transform.position, transform.up));
            }
        }