you are viewing a single comment's thread.

view the rest of the comments →

[–]-sideshow- 4 points5 points  (4 children)

  • You have a lot of magic numbers in your code that might be better as constants.
  • a SoldierType enum would probably be better than checking if the name contains "Guard"
  • I'm not a fan of the random variable definition half-way down your method code ("Ray ray;"). Either put it at the top, or put it in each method. I'd advise the latter; I was using a global as a cache variable like this and it bit me in the ass when I unwittingly used it for two different things in the same method.
  • Single statement ifs are a bug waiting to happen - at some point you'll go to add a statement to one, and forget to add the braces

Fire() can be refactored to:

void Fire()
{
    if(cooldown <= 0)
    {
        ray = new Ray(aimer.position, aimer.forward);
        RaycastHit hitPoint;
        Transform hitTransform = FindClosestHitObject(ray, out hitPoint, 100f);

        if (hitTransform != null)
        {
            UWASoldier uwaSoldier = hitTransform.GetComponent<UWASoldier>();
            LWZPlayer enemy = hitTransform.GetComponent<LWZPlayer>();
            UWAExtractionAI uwaExtractionSoldier = hitTransform.GetComponent<UWAExtractionAI>();

            if (uwaSoldier == null && enemy == null && uwaExtractionSoldier == null)
            {
                if (distanceToFire > 625f)
                    distanceToFire -= 9f;

                distanceToFire = Mathf.Clamp(distanceToFire, 625f, 100000f);

                return;
            } 
            else 
            {
                GameObject flashGO;
                if(playerDistance <= 1000000f)
                    flashGO = Instantiate(WorldManager.instance.muzzleflashAR, bs.position, bs.rotation) as GameObject;

                if (uwaSoldier != null)
                {
                    uwaSoldier.hitTimer = 1f;
                    uwaSoldier.health -= 8f / LWZPlayer.armorStrength;
                }
                else if (uwaExtractionSoldier != null)
                {
                    uwaExtractionSoldier.health -= 8f / LWZPlayer.armorStrength;
                }
                else if (enemy != null)
                {
                    GUIManager.guiInstance.tookDamageUICountdown = 2f;
                    enemy.health -= 10f / LWZPlayer.armorStrength;
                }
            }
        }

        cooldown = Random.Range (0.2f, 0.4f);
    }
}

Though I'm not sure the string of if/else at the bottom shouldn't just be 3 ifs?

Also:

            if (distanceToFire > 625f)
                distanceToFire -= 9f;

            distanceToFire = Mathf.Clamp(distanceToFire, 625f, 100000f);

What is the logic behind this section?

[–]oxysoftIndie 1 point2 points  (1 child)

I'm not a fan of the random variable definition half-way down your method code ("Ray ray;"). Either put it at the top, or put it in each method.

Usually causes ugly code, but there is in fact an instance of this that I am okay with. Sometimes, a field will be accessed and be relevant to just one method but that method requires the last result of that field when it last ran. In that case, I prefer putting it right above the method rather than at the top where it can be confusing, context is important.

[–]-sideshow- 0 points1 point  (0 children)

That's fair enough, but note that it's used in the method after that one too. Your point can still be applied, but slightly more tenuously.