Ejemplo n.º 1
0
    public static void ShootProjectile(ProjectileData projectile, float spread, float range, GameObject origin, Faction originFaction, Transform aimOrigin, Transform muzzle, Vector3 direction)
    {
        RaycastHit targetFound;
        Vector3    destination = Quaternion.Euler(Random.Range(-spread, spread), Random.Range(-spread, spread), Random.Range(-spread, spread)) * direction;

        if (Physics.Raycast(aimOrigin.position, destination, out targetFound, range, projectile.hitDetection)) // To reduce the amount of superfluous variables, I re-used the 'target' Vector3 in the same function as it is now unneeded for its original purpose
        {
            destination = targetFound.point;
        }
        else
        {
            destination = (direction * range) + destination;
            //destination.magnitude *= range;

            //Vector3 d = (direction + destination) * range;
            //destination = d;
            destination *= range;
        }

        Object.Instantiate(projectile.NewProjectile(origin, originFaction), muzzle.position, Quaternion.LookRotation(destination - muzzle.position, Vector3.up));
    }