Beispiel #1
0
        /// <summary>
        /// Go to the target and shoot at them.
        /// </summary>
        private void HandleFightingState()
        {
            // Iterate the time since we've attacked
            timeSinceAttack += Time.deltaTime;

            // Stand within attacking distance and attack the target
            if (target == null)
            {
                SwapState(AI_State.idle);
            }
            else
            {
                // Try to attack
                if (timeSinceAttack > attackFrequency)
                {
                    // Check if we can even see the target
                    var targetVisable = CanSeeObject(target);

                    if (targetVisable)
                    {
                        // If the target is visable we should attack them
                        inputManager.Fire1_performed();

                        // Wait at least one frame between attacks
                        timeSinceAttack = -Time.deltaTime;
                    }
                    else
                    {
                        // If the target is not visable we should move
                        var point = target.position;

                        // Clamp the position to our range
                        point.x = Mathf.Clamp(_transform.position.x, point.x - visabilityRange, point.x + visabilityRange);
                        point.z = Mathf.Clamp(_transform.position.z, point.z - visabilityRange, point.z + visabilityRange);

                        // Create a point that we can move to on the nav mesh
                        var moveTo = GetNavMeshPosition(point);

                        // If the position we picked is null, find a wander position instead
                        if (moveTo == Vector3.zero)
                        {
                            FindWanderPosition();
                        }
                        else
                        {
                            // Generate a path to the point
                            CalculatePath(moveTo);
                        }
                    }
                }
            }
        }