示例#1
0
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Select Weapons
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public IEnumerator SelectWeapon(int index)
    {
        FPSRigidBodyWalker FPSWalkerComponent = playerObj.GetComponent <FPSRigidBodyWalker>();

        //we are not dropping a weapon anymore if one has been selected
        dropWeapon = false;

        //do not proceed with selecting weapon if player doesn't have it in their inventory
        //but make an exception for the null/unarmed weapon for when the player presses the holster button
        //also dont allow weapon switch if player is climbing, swimming, or holding object and their weapon is lowered
        if ((!weaponOrder[index].GetComponent <WeaponBehavior>().haveWeapon&& index != 0) ||
            FPSWalkerComponent.hideWeapon)
        {
            yield break;
        }

        if (index != 0)        //if a weapon is selected, prevent unarmed/null weapon from being selected in selection cycle
        {
            weaponOrder[0].GetComponent <WeaponBehavior>().haveWeapon = false;
        }

        //set up external script references
        Ironsights     IronsightsComponent            = playerObj.GetComponent <Ironsights>();
        CameraKick     CameraKickComponent            = Camera.main.GetComponent <CameraKick>();
        FPSPlayer      FPSPlayerComponent             = playerObj.GetComponent <FPSPlayer>();
        WeaponBehavior CurrentWeaponBehaviorComponent = weaponOrder[currentWeapon].GetComponent <WeaponBehavior>();

        //cancel zooming when switching
        FPSPlayerComponent.zoomed = false;

        //reset non-magazine reload if interrupted by weapon switch
        if (CurrentWeaponBehaviorComponent.bulletsToReload != CurrentWeaponBehaviorComponent.bulletsPerClip &&
            CurrentWeaponBehaviorComponent.weaponMesh.GetComponent <Animation>()["Neutral"] &&
            IronsightsComponent.reloading)
        {
            //play neutral animation when putting weapon away to prevent neutral anim glitch at start of next reload
            CurrentWeaponBehaviorComponent.weaponMesh.GetComponent <Animation>()["Neutral"].speed = 1.5f;
            CurrentWeaponBehaviorComponent.weaponMesh.GetComponent <Animation>().Play("Neutral", PlayMode.StopAll);
            //reset bulletsReloaded to prevent delay of reloading the next time we reload this weapon
            CurrentWeaponBehaviorComponent.bulletsReloaded = 0;
        }

        //cancel reloading when switching
        IronsightsComponent.reloading = false;                  //set IronSights Reloading var to false
        CurrentWeaponBehaviorComponent.StopCoroutine("Reload"); //stop the Reload function if it is running

        //make timer active during switch to prevent shooting
        switchTime = Time.time - Time.deltaTime;

        if (Time.timeSinceLevelLoad > 2)
        {
            //play weapon switch sound if not the first call to this function after level load
            AudioSource.PlayClipAtPoint(changesnd, mainCamTransform.position);

            //play camera weapon switching animation
            Camera.main.GetComponent <Animation>().Rewind("CameraSwitch");
            Camera.main.GetComponent <Animation>().CrossFade("CameraSwitch", 0.35f, PlayMode.StopAll);
        }

        //if weapon uses rifle sprinting animation, set speed and play animation
        if (!CurrentWeaponBehaviorComponent.PistolSprintAnim)
        {
            //animate previous weapon down
            if (!FPSWalkerComponent.canRun)
            {
                weaponOrder[currentWeapon].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 0;
                weaponOrder[currentWeapon].GetComponent <Animation>()["RifleSprinting"].speed          = 1.5f;
                weaponOrder[currentWeapon].GetComponent <Animation>().CrossFade("RifleSprinting", 0.00025f, PlayMode.StopAll);
            }
            else
            {
                //if player is sprinting, keep weapon in sprinting position during weapon switch
                weaponOrder[currentWeapon].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1;
            }
        }
        else          //weapon uses pistol sprinting animation
                      //animate previous weapon down
        {
            if (!FPSWalkerComponent.canRun)
            {
                weaponOrder[currentWeapon].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 0;
                weaponOrder[currentWeapon].GetComponent <Animation>()["PistolSprinting"].speed          = 1.5f;
                weaponOrder[currentWeapon].GetComponent <Animation>().CrossFade("PistolSprinting", 0.00025f, PlayMode.StopAll);
            }
            else
            {
                //if player is sprinting, keep weapon in sprinting position during weapon switch
                weaponOrder[currentWeapon].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1;
            }
        }

        if (Time.timeSinceLevelLoad > 2)
        {
            if (CurrentWeaponBehaviorComponent.meleeSwingDelay == 0)
            {
                //move weapon down while switching
                IronsightsComponent.switchMove = -0.4f;
            }
            else
            {
                //move melee weapons down further while switching because they take more vertical screen space than guns
                IronsightsComponent.switchMove = -1.2f;
            }

            //wait for weapon down animation to play before switching weapons and animating weapon up
            yield return(new WaitForSeconds(0.2f));
        }

        //immediately switch weapons (activate called weaponOrder index and deactivate all others)
        for (int i = 0; i < weaponOrder.Length; i++)
        {
            if (i == index)
            {
                                #if UNITY_3_5
                // Activate the selected weapon
                weaponOrder[i].SetActiveRecursively(true);
                                #else
                // Activate the selected weapon
                weaponOrder[i].SetActive(true);
                                #endif

                //get current weapon value from index
                currentWeapon = index;

                //synchronize current and previous weapon's y pos for correct offscreen switching, use localPosition not position for correct transforms
                weaponOrder[i].transform.localPosition = weaponOrder[i].transform.localPosition + new Vector3(0, weaponOrder[i].transform.localPosition.y - 0.3f, 0);

                if (Time.timeSinceLevelLoad > 2)
                {
                    //move weapon up when switch finishes
                    IronsightsComponent.switchMove = 0;
                }

                //if weapon uses rifle sprinting animation set speed and animate
                if (!weaponOrder[i].GetComponent <WeaponBehavior>().PistolSprintAnim)
                {
                    //animate selected weapon up by setting time of animation to it's end and playing in reverse
                    if (!FPSWalkerComponent.canRun)
                    {
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].speed          = -1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("RifleSprinting", 0.00025f, PlayMode.StopAll);
                    }
                    else
                    {
                        //if player is sprinting, keep weapon in sprinting position during weapon switch
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].speed          = 1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("RifleSprinting", 0.00025f, PlayMode.StopAll);
                    }
                }
                else                  //weapon uses pistol sprinting animation
                                      //animate selected weapon up by setting time of animation to it's end and playing in reverse
                {
                    if (!FPSWalkerComponent.canRun)
                    {
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].speed          = -1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("PistolSprinting", 0.00025f, PlayMode.StopAll);
                    }
                    else
                    {
                        //if player is sprinting, keep weapon in sprinting position during weapon switch
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1.0f;
                        weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].speed          = 1.5f;
                        weaponOrder[i].GetComponent <Animation>().CrossFade("PistolSprinting", 0.00025f, PlayMode.StopAll);
                    }
                }

                //update transform reference of active weapon object in other scipts
                IronsightsComponent.gun = weaponOrder[i].transform;
                //update active weapon object reference in other scipts
                IronsightsComponent.gunObj = weaponOrder[i];
                CameraKickComponent.gun    = weaponOrder[i];
            }
            else
            {
                //reset transform of deactivated gun to make it in neutral position when selected again
                //use weapon parent transform.position instead of Camera.main.transform.position
                //or Camera.main.transform.localPosition to avoid positioning bugs due to camera pos changing with walking bob and kick
                weaponOrder[i].transform.position = myTransform.position;

                if (!weaponOrder[i].GetComponent <WeaponBehavior>().PistolSprintAnim)               //weapon uses rifle sprinting animation
                //reset animation
                {
                    weaponOrder[i].GetComponent <Animation>()["RifleSprinting"].normalizedTime = 1.0f;
                }
                else                  //weapon uses pistol sprinting animation
                                      //reset animation
                {
                    weaponOrder[i].GetComponent <Animation>()["PistolSprinting"].normalizedTime = 1.0f;
                }
                //synchronize sprintState var in WeaponBehavior script
                weaponOrder[i].GetComponent <WeaponBehavior>().sprintState = true;

                                #if UNITY_3_5
                // Activate the selected weapon
                weaponOrder[i].SetActiveRecursively(false);
                                #else
                // Activate the selected weapon
                weaponOrder[i].SetActive(false);
                                #endif
            }
        }
    }
示例#2
0
    void Update()
    {
        //Set up external script references
        FPSRigidBodyWalker FPSWalkerComponent  = playerObj.GetComponent <FPSRigidBodyWalker>();
        Ironsights         IronsightsComponent = playerObj.GetComponent <Ironsights>();
        FPSPlayer          FPSPlayerComponent  = playerObj.GetComponent <FPSPlayer>();
        CameraKick         CameraKickComponent = Camera.main.GetComponent <CameraKick>();
        Footsteps          FootstepsComponent  = playerObj.GetComponent <Footsteps>();

        if (Time.timeScale > 0 && Time.deltaTime > 0)        //allow pausing by setting timescale to 0

        {
            waveslice = 0.0f;
            if (!FPSPlayerComponent.useAxisInput)
            {
                horizontal = FPSWalkerComponent.inputX;                //get input from player movement script
                vertical   = FPSWalkerComponent.inputY;
            }
            else
            {
                horizontal = FPSWalkerComponent.inputXSmoothed;                //get input from player movement script
                vertical   = FPSWalkerComponent.inputYSmoothed;
            }
            midpoint = FPSWalkerComponent.midPos;                                                      //Start bob from view position set in player movement script

            if (Mathf.Abs(horizontal) != 0 || Mathf.Abs(vertical) != 0 && FPSWalkerComponent.grounded) //Perform bob only when moving and grounded

            {
                waveslice     = Mathf.Sin(timer);
                wavesliceRoll = Mathf.Sin(timerRoll);
                if (Mathf.Abs(FPSWalkerComponent.inputY) > 0.1f)
                {
                    inputSpeed = Mathf.Abs(FPSWalkerComponent.inputY);
                }
                else
                {
                    inputSpeed = Mathf.Abs(FPSWalkerComponent.inputX);
                }
                timer     = timer + bobbingSpeed * inputSpeed * Time.deltaTime;
                timerRoll = timerRoll + (bobbingSpeed / 2.0f) * Time.deltaTime;                //Make view roll bob half the speed of view pitch bob

                if (timer > Mathf.PI * 2.0f)
                {
                    timer = timer - (Mathf.PI * 2.0f);
                    if (!FPSWalkerComponent.noClimbingSfx)                    //dont play climbing footsteps if noClimbingSfx is true
                    {
                        FootstepsComponent.FootstepSfx();                     //play footstep sound effect by calling FootstepSfx() function in Footsteps.cs
                    }
                }

                //Perform bobbing of camera roll
                if (timerRoll > Mathf.PI * 2.0f)
                {
                    timerRoll = (timerRoll - (Mathf.PI * 2.0f));
                    if (!FPSWalkerComponent.grounded)
                    {
                        timerRoll = 0;                        //reset timer when airborne to allow soonest resume of footstep sfx
                    }
                }
            }
            else
            {
                //reset variables to prevent view twitching when falling
                timer                = 0.0f;
                timerRoll            = 0.0f;
                tempLocalEulerAngles = new Vector3(0, 0, 0);              //reset camera angles to 0 when stationary
            }

            if (waveslice != 0)
            {
                translateChange      = waveslice * bobbingAmount;
                translateChangePitch = waveslice * pitchingAmount;
                translateChangeRoll  = wavesliceRoll * rollingAmount;
                translateChangeYaw   = wavesliceRoll * yawingAmount;
                totalAxes            = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
                totalAxes            = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
                //needed for smooth return to neutral view pos
                translateChange      = totalAxes * translateChange;
                translateChangePitch = totalAxes * translateChangePitch;
                translateChangeRoll  = totalAxes * translateChangeRoll;
                //Set position for smoothing function and add jump value
                //divide position by deltaTime for framerate independence
                dampTo = midpoint + (translateChange / Time.deltaTime * 0.01f);
                //camera roll and pitch bob
                tempLocalEulerAngles = new Vector3(translateChangePitch, translateChangeYaw, translateChangeRoll);
            }
            else
            {
                if (!FPSWalkerComponent.swimming)
                {
                    if (IronsightsComponent.cameraIdleBob)
                    {
                        idleYBob = Mathf.Sin(Time.time * 1.25f) * 0.015f;
                    }
                }
                else
                {
                    if (IronsightsComponent.cameraSwimBob)
                    {
                        idleYBob = Mathf.Sin(Time.time * 1.65f) * 0.08f;                        //increase vertical bob when swimming
                    }
                }

                //reset variables to prevent view twitching when falling
                dampTo          = midpoint + idleYBob;       //add small sine bob for camera idle movement
                totalAxes       = 0;
                translateChange = 0;
            }
            //use SmoothDamp to smooth position and remove any small glitches in bob amount
            dampOrg = Mathf.SmoothDamp(dampOrg, dampTo, ref dampVelocity, 0.1f, Mathf.Infinity, Time.deltaTime);
            //Pass bobbing amount and angles to the camera kick script in the camera object after smoothing
            CameraKickComponent.dampOriginY = dampOrg;
            CameraKickComponent.bobAngles   = Vector3.SmoothDamp(CameraKickComponent.bobAngles, tempLocalEulerAngles, ref dampVelocity2, 0.1f, Mathf.Infinity, Time.deltaTime);
        }
    }
示例#3
0
    void Update()
    {
        //set up external script references
        FPSRigidBodyWalker FPSWalkerComponent  = playerObj.GetComponent <FPSRigidBodyWalker>();
        FPSPlayer          FPSPlayerComponent  = playerObj.GetComponent <FPSPlayer>();
        CameraKick         CameraKickComponent = Camera.main.GetComponent <CameraKick>();

        if (Time.timeScale > 0 && Time.deltaTime > 0)        //allow pausing by setting timescale to 0

        {
            waveslice = 0.0f;
            if (!FPSPlayerComponent.useAxisInput)
            {
                horizontal = FPSWalkerComponent.inputX;                //get input from player movement script
                vertical   = FPSWalkerComponent.inputY;
            }
            else
            {
                horizontal = FPSWalkerComponent.inputXSmoothed;                //get input from player movement script
                vertical   = FPSWalkerComponent.inputYSmoothed;
            }

            if (Mathf.Abs(horizontal) != 0 || Mathf.Abs(vertical) != 0 && FPSWalkerComponent.grounded)             //Perform bob only when moving and grounded
            {
                waveslice = Mathf.Sin(timer);
                if (Mathf.Abs(FPSWalkerComponent.inputY) > 0.1f)
                {
                    inputSpeed = Mathf.Abs(FPSWalkerComponent.inputY);
                }
                else
                {
                    inputSpeed = Mathf.Abs(FPSWalkerComponent.inputX);
                }
                timer = timer + bobbingSpeed * inputSpeed * Time.deltaTime;
                if (timer > Mathf.PI * 2.0f)
                {
                    timer = timer - (Mathf.PI * 2.0f);
                }
            }
            else
            {
                timer = 0.0f;                //reset timer when stationary to start bob cycle from neutral position
            }

            if (waveslice != 0)
            {
                translateChange = waveslice * bobbingAmount;
                totalAxes       = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
                totalAxes       = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
                translateChange = totalAxes * translateChange;
                //set position for smoothing function
                dampTo = translateChange / Time.deltaTime * 0.01f;                //divide position by deltaTime for framerate independence
            }
            else
            {
                //reset variables to prevent view twitching when falling
                dampTo          = 0.0f;
                totalAxes       = 0.0f;
                translateChange = 0.0f;
            }
            //use SmoothDamp to smooth position and remove any small glitches in bob amount
            dampOrg = Mathf.SmoothDamp(dampOrg, dampTo, ref dampVelocity, 0.1f, Mathf.Infinity, Time.deltaTime);
            //pass bobbing amount to the camera kick script in the camera object after smoothing
            CameraKickComponent.dampOriginX = dampOrg;
        }
    }