Example #1
0
    new void Start()
    {
        base.Start();

        float t = InterceptSolverNoAccel.FindRealSolutionSmallestT(this, targetDrive); //print(t + Time.time);

        if (targetDrive == null || float.IsInfinity(t))                                // Shooting at nothing or cannot hit the target
        {
            rb.velocity += transform.forward.normalized * speed;
            return;
        }



        Vector3 rp = targetDrive.EstimatedPos(t) - rb.position;

        Vector3 projectedWastedVel = Vector3.Project(rb.velocity, rp);
        Vector3 wastedVel          = -(rb.velocity - projectedWastedVel);
        Vector3 towardsTargetVel   = rp.normalized * Mathf.Sqrt(speed * speed - wastedVel.sqrMagnitude);
        Vector3 resultVel          = wastedVel + towardsTargetVel;



        rb.velocity       += resultVel;
        transform.rotation = Quaternion.LookRotation(resultVel);


        /*
         * var g = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         * g.transform.position = targetDrive.EstimatedPos(t);
         * Destroy(g.GetComponent<SphereCollider>()); */
    }
Example #2
0
    public void PDCUpdate()
    {
        if (target == null) // No current target
        {
            return;
        }

        if (target != null && targetDrive == null) // New target acqusition
        {
            targetDrive = target.GetComponent <Drive>();
            updateEstimatedIntercept();

            transferingTarget = Quaternion.Angle(transform.rotation, targetRot) < maxTargetTransferAngle;
            roundsRemaining   = Random.Range(magSizeRange[0], magSizeRange[1]);
        }



        updateEstimatedIntercept();
        transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, angularVelocity * Time.deltaTime);
        transform.rotation = Quaternion.LookRotation(transform.forward, transform.parent.up); // Prevent weird rotations

        if (parentPDCContoller.TargetInRange(transform, target))                              // Only shoot when target is in range
        {
            if (Quaternion.Angle(transform.rotation, targetRot) < maxDeadzoneAngle)
            {
                ShootRound(true);

                if (--roundsRemaining == 0) // Stop firing on current target, could also stop from round destroying target
                {
                    ClearTarget();
                }
            }
            else if (transferingTarget) // Shooting just for looks
            {
                ShootRound(false);
            }
        }



        void updateEstimatedIntercept()
        {
            float predictedT = InterceptSolverNoAccel.FindRealSolutionSmallestT(
                parentDrive.rb.velocity, roundSpawnPoint.position, pdcRoundSpeed, targetDrive);

            if (float.IsInfinity(predictedT)) // Can never hit the target
            {
                ClearTarget();
                return;
            }



            // 99% Sure these calculations are slightly off
            Vector3 rp = targetDrive.EstimatedPos(predictedT) - roundSpawnPoint.position;

            Vector3 projectedWastedVel = Vector3.Project(parentDrive.rb.velocity, rp);
            Vector3 wastedVel          = -(parentDrive.rb.velocity - projectedWastedVel);
            Vector3 towardsTargetVel   = rp.normalized * Mathf.Sqrt(pdcRoundSpeed * pdcRoundSpeed - wastedVel.sqrMagnitude);

            targetRot = Quaternion.LookRotation(wastedVel + towardsTargetVel, transform.parent.up);
        }
    }