Example #1
0
        private void handleAttackLogic()
        {
            // If the creature has a target, attempt to move towards it.
            if (targetingBehaviour.HasCurrentTarget)
            {
                CreatureTarget creatureTarget = targetingBehaviour.CurrentTarget.Value;

                // If the target is out of range, move towards it.
                if (Vector3.Distance(creatureTarget.Transform.position, transform.position) > Range)
                {
                    movementBehaviour.StoppingDistance = Range;
                    movementBehaviour.SetTarget(creatureTarget.Transform);
                }
                // Otherwise; attempt to attack the target.
                else
                {
                    // Stop moving.
                    movementBehaviour.StopMoving();

                    // Face the target.
                    transform.LookAt(creatureTarget.Transform);

                    // If it has been long enough since the last attack, attack again.
                    if (timeSinceLastAttack >= AttackInterval && creatureTarget.Target.IsAlive)
                    {
                        // Deal the damage to the target.
                        creatureTarget.Target.Health -= Damage;

                        // Add the dealt damage to the stat.
                        changeLifetimeStat("EnemyDamageDealt", Damage);

                        // If the target is now dead, increment the kill counter.
                        if (!creatureTarget.Target.IsAlive)
                        {
                            changeLifetimeStat("EnemyKills", 1);
                        }

                        // Invoke the attack event.
                        onAttack.Invoke();

                        // Reset the attack timer.
                        timeSinceLastAttack = 0;
                    }
                }
            }
            // Otherwise, just move towards the goal.
            else
            {
                movementBehaviour.SetTarget(Creature.GoalObject);
                movementBehaviour.StoppingDistance = 0;
            }

            // Add the elapsed time to the attack timer.
            timeSinceLastAttack += Time.fixedDeltaTime;
        }
Example #2
0
        private void handleAttackLogic()
        {
            // Add the elapsed time to the attack timer.
            timeSinceLastAttack += Time.fixedDeltaTime;

            // If the creature has a target, attempt to move towards it.
            if (targetingBehaviour.HasCurrentTarget)
            {
                CreatureTarget creatureTarget = targetingBehaviour.CurrentTarget.Value;

                // Stop moving.
                movementBehaviour.StopMoving();

                // Face the target.
                transform.LookAt(creatureTarget.Transform);

                // If it has been long enough since the last attack, attack again.
                if (timeSinceLastAttack >= AttackInterval && creatureTarget.Target.IsAlive)
                {
                    // Calculate the power of the throw.
                    float power = Mathf.Clamp01(PowerBias + creatureTarget.NormalisedHealth * PowerHealthWeight + creatureTarget.NormalisedDistance * PowerDistanceWeight);

                    // Calculate the pitch of the throw.
                    float pitch = Mathf.Clamp(PitchBias + creatureTarget.NormalisedDistance * PitchDistanceWeight + NeuralNetworkHelper.ExpandRangeToNegative(creatureTarget.TargetRigidbody.velocity.magnitude / 1000) * PitchSpeedWeight
                                              + power * PitchPowerWeight, -1, 1);

                    // Calculate the yaw of the throw.
                    float yaw = Mathf.Clamp(YawBias + creatureTarget.NormalisedDistance * YawDistanceWeight + power * YawPowerWeight
                                            + NeuralNetworkHelper.ExpandRangeToNegative(creatureTarget.TargetRigidbody.velocity.magnitude / 1000) * YawSpeedWeight, -1, 1);

                    // Calcualte the normalised direction to the target, then the angles (in degrees) from that.
                    Vector3 direction = Quaternion.LookRotation((creatureTarget.Transform.position - transform.position).normalized).eulerAngles;
                    direction.y += yaw * Mathf.Clamp(YawDeviation, 0, 180);
                    direction.x += pitch * Mathf.Clamp(PitchDeviation, 0, 180);

                    // Create a new spear.
                    GameObject spearObject = Instantiate(projectilePrefab.gameObject, heldWeapon.position, Quaternion.Euler(direction), Creature.ProjectileContainer);

                    //spearObject.transform.position = heldWeapon.position;
                    spearObject.transform.localScale = Vector3.Scale(heldWeapon.localScale, transform.localScale);

                    // Get the projectile component from the created spear.
                    Projectile spearProjectile = spearObject.GetComponent <Projectile>();

                    spearProjectile.InitialiseProjectile(onProjectileHitEnemy, Creature, creatureTarget.Target, creatureTarget.Transform.position, spearObject.transform.position);

                    // Add the force to the spear.
                    spearProjectile.Rigidbody.AddForce(spearObject.transform.forward * (power + 1) * MaxPower, ForceMode.Impulse);

                    // Increment the thrown shots counter.
                    thrownShots++;

                    // Reset the attack timer.
                    timeSinceLastAttack = 0;
                }
            }
            // Otherwise, just move towards the goal.
            else
            {
                movementBehaviour.SetTarget(Creature.GoalObject);
                movementBehaviour.StoppingDistance = 0;
            }
        }