Exemplo n.º 1
0
        public void HearSound(Vector3 soundPos)
        {
            if (myAIBaseScript && shouldReactToNewSound && !myAIBaseScript.IsEnaging())
            {
                //StackTrace st = new StackTrace();
                //UnityEngine.Debug.Log(st.GetFrame(1).GetMethod().Name);
                //UnityEngine.Debug.Break();

                CheckForLOSAwareness(true);
                myAIBaseScript.StartCoroutine("HearSound", soundPos);
                myAIBaseScript.SetAlertSpeed();
                StartCoroutine(SetTimeUntilNextSound());
            }
        }
Exemplo n.º 2
0
        //Rotating
        void RotateAI()
        {
            //Rotate to look in the given direction, if one is given.
            if (useCustomRotation)
            {
                newRotation                = Quaternion.LookRotation(directionToFace);
                newRotation.eulerAngles    = new Vector3(0.0f, newRotation.eulerAngles.y, 0.0f);
                myAIBodyTransform.rotation = Quaternion.Lerp(myAIBodyTransform.rotation, newRotation, turnSpeed * Time.deltaTime);
            }
            else if ((myBaseScript.IsEnaging() && myBaseScript.targetTransform && !sprinting))
            {
                //Get angle between vector of movement and the actual direction enemyBody is facing
                myAngle = Vector3.Angle(myTransform.forward, myAIBodyTransform.forward);

                if (Vector3.Angle(-myAIBodyTransform.right, myTransform.forward) > 90)
                {
                    myAngle = -myAngle;
                }

                //Get angle between vector of movement and the direction we want to be facing
                float angleBetweenFor = Vector3.Angle(myTransform.forward, myBaseScript.targetTransform.position - myAIBodyTransform.position);

                //The following if statement is to even out clipping and crossfading problems with ~45 degree angle strafing.
                //If the angle between the direction we are moving in and the vector to the target will commonly result in clipping,
                //then we face the legs in the direction of movement, play either the forwards or backwards animations and
                // rely on the chest movement to aim at the target.

                //We will also always rotate to fact the target if the speed is too low,
                //because while standing still, the vector of movement becomes unreliable.
                if (angleBetweenFor > minAngleToRotateBase && angleBetweenFor < 180 - minAngleToRotateBase)
                {
                    newRotation = Quaternion.LookRotation(myBaseScript.targetTransform.position - myAIBodyTransform.position);
                }
                else
                {
                    //Play correct animation
                    if (angleBetweenFor < 90)
                    {
                        newRotation = Quaternion.LookRotation(myTransform.forward);
                        animator.SetFloat(forwardsMoveHash, Vector3.Magnitude(agent.desiredVelocity) / maxMovementSpeed, animationDampTime, Time.deltaTime);
                        animator.SetFloat(sidewaysMoveHash, 0, animationDampTime, Time.deltaTime);
                    }
                    else
                    {
                        newRotation = Quaternion.LookRotation(-myTransform.forward);
                        animator.SetFloat(forwardsMoveHash, -Vector3.Magnitude(agent.desiredVelocity) / maxMovementSpeed, animationDampTime, Time.deltaTime);
                        animator.SetFloat(sidewaysMoveHash, 0, animationDampTime, Time.deltaTime);
                    }
                }

                //Make sure we only rotate around the y axis
                newRotation.eulerAngles = new Vector3(0.0f, newRotation.eulerAngles.y, 0.0f);

                //Smoothly rotate to face target
                myAIBodyTransform.rotation = Quaternion.Slerp(myAIBodyTransform.rotation, newRotation, Time.deltaTime * turnSpeed);
            }
            //Look in the direction we are moving.
            else
            {
                myAngle = 0;

                newRotation             = Quaternion.LookRotation(myTransform.forward);
                newRotation.eulerAngles = new Vector3(0.0f, newRotation.eulerAngles.y, 0.0f);

                myAIBodyTransform.rotation = Quaternion.Lerp(myAIBodyTransform.rotation, newRotation, turnSpeed * Time.deltaTime);
            }
        }
Exemplo n.º 3
0
        //Shooting////////////////////////////////////////////////////////
        IEnumerator BulletFiringCycle()
        {
            //Fire
            isFiring = true;

            //Wait for the animation transitioning the agent from hiding to a firing positiont o finish.
            if (myAIBaseScript.inCover)
            {
                yield return(new WaitForSeconds(coverTransitionTime));
            }

            //Don't fire if the agent is unaware of the target or meleeing the target.
            if (myAIBaseScript.IsEnaging() && !myAIBaseScript.isMeleeing)
            {
                //If we have clear LoS to the LOSTargetTransform, fire
                //You may want to check for line of sight to a position right above the target's head (for example)
                //This will allow your agent to lay down suppressing fire even if they can't see the target.
                if (LOSTargetTransform && !animationScript.isSprinting())
                {
                    //See if we can use our secondary fire
                    //While a grenade may not need LoS, a homing missile might
                    if (!Physics.Linecast(bulletSpawn.position, LOSTargetTransform.position, LOSLayermask))
                    {
                        lastPosTargetSeen   = targetTransform.position;
                        canFireGrenadeAgain = true;
                        FireOneGrenade();
                        canFireGrenadeAgain = true;
                    }
                    else if (!needsLOSForSecondaryFire)
                    {
                        if (canFireGrenadeAgain)
                        {
                            FireOneGrenade();
                        }
                        canFireGrenadeAgain = true;
                    }
                }
                //Create the sound that will be heard by Paragon AI agents
                //This sound is not going to be heard by the player
                if (soundRadius > 0)
                {
                    ParagonAI.ControllerScript.currentController.CreateSound(bulletSpawn.position, soundRadius, enemyTeams);
                }
                //Shoot regular bullets
                yield return(StartCoroutine(Fire()));
            }

            //Transition
            isWaiting = true;
            isFiring  = false;


            //If we aren't reloading wait for a while before firing another burst
            if (currentBulletsUntilReload > 0 && reloadTime > 0)
            {
                yield return(new WaitForSeconds(minPauseTime + Random.value * randomPauseTimeAdd));
            }
            else
            {
                //If we're out of ammo, reload.
                if (reloadSound)
                {
                    audioSource.volume = reloadSoundVolume;
                    audioSource.PlayOneShot(reloadSound);
                }
                if (animationScript)
                {
                    animationScript.PlayReloadAnimation();
                }
                if (soundScript)
                {
                    soundScript.PlayReloadAudio();
                }
                yield return(new WaitForSeconds(reloadTime));

                currentBulletsUntilReload = bulletsUntilReload;
                yield return(new WaitForSeconds(minPauseTime * Random.value));
            }
            isWaiting = false;
        }