Ejemplo n.º 1
0
        public void Spawn(GameObject projectilePrefab, FiringSolution solution)
        {
            var body          = Instantiate(projectilePrefab);
            var projectile    = body.GetComponent <ProjectileComponent>();
            var spawnPosition = transform.position;

            projectile.Shoot(spawnPosition, solution);
        }
Ejemplo n.º 2
0
 protected void ShootProjectile(FiringSolution solution)
 {
     foreach (var barrel in _barrels)
     {
         barrel.Spawn(ProjectilePrefab, solution);
     }
     OnShot();
 }
Ejemplo n.º 3
0
        public void Shoot(Vector3 position, FiringSolution solution)
        {
            var body = GetComponent <Rigidbody>();

            transform.position = _startPosition = position;
            transform.forward  = solution.FiringDirection;
            body.MovePosition(position);
            body.velocity = Velocity * solution.FiringDirection;
            Solution      = solution;
        }
Ejemplo n.º 4
0
        private static FiringSolution?TrySolve(Vector3 projectilePosition,
                                               float projectileVelocity,
                                               float projectileRange,
                                               Vector3 targetPosition,
                                               Vector3 targetVelocity, float targetAcceleration)
        {
            // We need to solve "v1*t = a2*t + v2*t"
            // Where v1 is our projectile's speed, v2 is the rocket's speed and a2 is the rocket's acceleration.
            // The following solution is a simple iterative approach that starts with a wrong solution, calculates
            // the error and then tries to find the solution where the error is minimal.

            var            distance   = Vector3.Distance(projectilePosition, targetPosition);
            var            flyingTime = distance / projectileVelocity;
            FiringSolution?solution   = null;

            const int maxIterations = 10;

            for (var i = 0; i < maxIterations; ++i)
            {
                // x = at²+vt
                var futurePosition = targetPosition +
                                     targetVelocity * flyingTime +
                                     targetAcceleration * Mathf.Pow(flyingTime, 2) * targetVelocity.normalized;
                var firingDirection = futurePosition - projectilePosition;
                var finalDistance   = firingDirection.magnitude;
                var finalFlyingTime = finalDistance / projectileVelocity;

                if (IsProperSolution(projectilePosition, targetPosition, futurePosition, finalDistance, projectileRange))
                {
                    solution = new FiringSolution
                    {
                        TargetPosition       = futurePosition,
                        FiringDirection      = firingDirection / finalDistance,
                        InterceptionDistance = finalDistance
                    }
                }
                ;

                flyingTime = finalFlyingTime;
            }

            return(solution);
        }