예제 #1
0
        void Update()
        {
            if (!canLock)
            {
                return;
            }

            if (Input.GetMouseButton(0) && Screen.lockCursor == false)
            {
                SetPause(false);
            }

            if (InputDB.GetButton("Escape"))
            {
                SetPause(true);
            }

            // Did we lose cursor locking?
            // eg. because the user pressed escape
            // or because he switched to another application
            // or because some script set Screen.lockCursor = false;
            if (!Screen.lockCursor && wasLocked)
            {
                wasLocked = false;
                SetPause(true);
            }
            // Did we gain cursor locking?
            else if (Screen.lockCursor && !wasLocked)
            {
                wasLocked = true;
                SetPause(false);
            }
        }
        // Update is called once per frame
        void Update()
        {
            // Get the input vector from kayboard or analog stick
            Vector3 directionVector = new Vector3(InputDB.GetAxis("Horizontal"), 0, InputDB.GetAxis("Vertical"));

            if (directionVector != Vector3.zero)
            {
                // Get the length of the directon vector and then normalize it
                // Dividing by the length is cheaper than normalizing when we already have the length anyway
                float directionLength = directionVector.magnitude;
                directionVector = directionVector / directionLength;

                // Make sure the length is no bigger than 1
                directionLength = Mathf.Min(1, directionLength);

                // Make the input vector more sensitive towards the extremes and less sensitive in the middle
                // This makes it easier to control slow speeds when using analog sticks
                directionLength = directionLength * directionLength;

                // Multiply the normalized direction vector by the modified length
                directionVector = directionVector * directionLength;
            }

            // Apply the direction to the CharacterMotorDB
            motor.inputMoveDirection = transform.rotation * directionVector;
            motor.inputJump          = InputDB.GetButton("Jump");
        }
예제 #3
0
        void FixedUpdate()
        {
            if (grounded && PlayerWeapons.canMove)
            {
                // We are grounded, so recalculate movedirection directly from axes
                moveDirection  = new Vector3(InputDB.GetAxis("Horizontal"), 0, InputDB.GetAxis("Vertical"));
                moveDirection  = transform.TransformDirection(moveDirection);
                moveDirection *= speed;

                if (InputDB.GetButton("Jump"))
                {
                    moveDirection.y = jumpSpeed;
                    if (crouching)
                    {
                        stopCrouching = true;
                        NormalSpeed();
                    }
                }
            }

            // Apply gravity
            moveDirection.y -= gravity * Time.deltaTime;

            // Move the controller
            CharacterController controller = GetComponent <CharacterController>();
            CollisionFlags      flags      = controller.Move(moveDirection * Time.deltaTime);

            grounded = (flags & CollisionFlags.CollidedBelow) != 0;

            if ((Mathf.Abs(moveDirection.x) > 0) && grounded || (Mathf.Abs(moveDirection.z) > 0 && grounded))
            {
                if (!walking)
                {
                    walking = true;
                    BroadcastMessage("Walking", SendMessageOptions.DontRequireReceiver);
                }
            }
            else if (walking)
            {
                walking         = false;
                stopWalkingTime = Time.time;
                BroadcastMessage("StopWalking", SendMessageOptions.DontRequireReceiver);
            }
        }
예제 #4
0
        void Update()
        {
            if (!playerActive)
            {
                return;
            }

            /************************Interact****************************/
            if (interactDistance > 0)
            {
                //Set up ray
                Ray        ray;
                RaycastHit hit;
                ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f, 0));
                if (Physics.Raycast(ray, out hit, interactDistance, interactMask))
                {
                    //We hit something new
                    if (lastHit != hit.transform)
                    {
                        //Last object isn't still highlighted
                        if (lastHit)
                        {
                            lastHit.SendMessage("HighlightOff", SendMessageOptions.DontRequireReceiver);
                        }
                        //New one is
                        lastHit = hit.transform;
                        lastHit.SendMessage("HighlightOn", SendMessageOptions.DontRequireReceiver);
                    }
                }
                else if (lastHit != null)
                {
                    //We hit nothing, but still have a object highlighted, so unhighlight it
                    lastHit.SendMessage("HighlightOff", SendMessageOptions.DontRequireReceiver);
                    lastHit = null;
                }

                //Interact
                if (InputDB.GetButtonDown("Interact") && lastHit != null)
                {
                    lastHit.SendMessage("Interact", this.gameObject, SendMessageOptions.DontRequireReceiver);
                }
            }
            /**********************Fire & Reload******************************/

            // Did the user press fire?
            if (InputDB.GetButton("Fire1") && (autoFire || charge) && canFire)
            {
                transform.root.BroadcastMessage("Fire", SendMessageOptions.DontRequireReceiver);
            }
            else if (InputDB.GetButtonDown("Fire1") && canFire)
            {
                transform.root.BroadcastMessage("Fire", SendMessageOptions.DontRequireReceiver);
            }
            if (InputDB.GetButton("Fire2") && canFire && (autoFire || charge))
            {
                BroadcastMessage("Fire2", SendMessageOptions.DontRequireReceiver);
            }
            else if (InputDB.GetButtonDown("Fire2") && canFire)
            {
                BroadcastMessage("Fire2", SendMessageOptions.DontRequireReceiver);
            }

            if (InputDB.GetButtonDown("Reload"))
            {
                BroadcastMessage("Reload", SendMessageOptions.DontRequireReceiver);
            }

            /*************************Weapon Switching***************************/

            if (!AimMode.canSwitchWeaponAim || hidden || !canSwitchWeapon || Avoidance.collided)
            {
                return;
            }

            if (InputDB.GetButtonDown("SelectWeapon"))
            {
                int temp = WeaponSelector.selectedWeapon;
                if (weapons.Length > temp && (selectedWeapon != temp || weapons[selectedWeapon] == null) && temp >= 0)
                {
                    if (weapons[temp] != null)
                    {
                        if (!weapons[temp].gameObject.GetComponent <WeaponInfo>().locked)
                        {
                            SelectWeapon(temp);
                            selectedWeapon = temp;
                            displayMessage = false;
                        }
                        else
                        {
                            SlotEmptyMessage(temp + 1);
                        }
                    }
                    else
                    {
                        SlotEmptyMessage(temp + 1);
                    }
                }
            }
        }
예제 #5
0
        void Update()
        {
            if (!GunScript1.gunActive)
            {
                if (transform.localPosition != hipPosition)
                {
                    transform.localPosition = hipPosition;
                }
                if (transform.localEulerAngles != hipRotation)
                {
                    transform.localEulerAngles = hipRotation;
                }
                sprinting = false;
                return;
            }

            staticAiming = aiming;

            //Replenish Sprint time
            float tempSprintTime = 0;

            if (controller.velocity.magnitude == 0)
            {
                tempSprintTime = sprintEndTime;
            }
            if (sprintNum < sprintDuration && !sprinting && Time.time > tempSprintTime)
            {
                if (controller.velocity.magnitude == 0)
                {
                    sprintNum = Mathf.Clamp(sprintNum + sprintAddStand * Time.deltaTime, 0, sprintDuration);
                }
                else
                {
                    sprintNum = Mathf.Clamp(sprintNum + sprintAddWalk * Time.deltaTime, 0, sprintDuration);
                }
            }
            if (sprintNum > sprintMin)
            {
                exhausted = false;
            }

            //Turn on scope if it is time
            if ((inScope && !aiming) || (zoomed && !aiming))
            {
                inScope = false;
                zoomed  = false;
                var gos = GetComponentsInChildren <Renderer>();
                foreach (Renderer go in gos)
                {
                    if (go.name != "muzzle_flash")
                    {
                        go.enabled = true;
                    }
                }
            }
            //Reset Camera
            if (!aiming && cmra.GetComponent <Camera>().fieldOfView != PlayerWeapons.fieldOfView)
            {
                if (sightsZoom1 && !scoped)
                {
                    cmra.GetComponent <Camera>().fieldOfView  = Mathf.Lerp(cmra.GetComponent <Camera>().fieldOfView, PlayerWeapons.fieldOfView, Mathf.SmoothStep(0, 1, 1 - (aimStartTime - Time.time) / aimRate));
                    wcmra.GetComponent <Camera>().fieldOfView = Mathf.Lerp(wcmra.GetComponent <Camera>().fieldOfView, PlayerWeapons.fieldOfView, Mathf.SmoothStep(0, 1, 1 - (aimStartTime - Time.time) / aimRate));
                }
                else
                {
                    cmra.GetComponent <Camera>().fieldOfView  = PlayerWeapons.fieldOfView;
                    wcmra.GetComponent <Camera>().fieldOfView = PlayerWeapons.fieldOfView;
                }
            }

            staticRate = aimRate;
            //aiming
            if (InputDB.GetButton("Aim") && canAim && PlayerWeapons.canAim && selected && !sprinting /*&& !GunScript1.sprint*/ && Avoidance.canAim)
            {
                if (!aiming)
                {
                    aimStartTime       = Time.time + aimRate;
                    scopeTime          = Time.time + aimRate;
                    aiming             = true;
                    canSwitchWeaponAim = false;
                    startPosition      = transform.localPosition;
                    startRotation      = transform.localEulerAngles;
                    curVect            = aimPosition - transform.localPosition;

                    player.BroadcastMessage("Aiming", zoomFactor, SendMessageOptions.DontRequireReceiver);
                }

                //Align to position
                GunToRotation(aimRotation, aimRate);
                if (aiming)
                {
                    transform.localPosition = Vector3.Slerp(startPosition, aimPosition, Mathf.SmoothStep(0, 1, 1 - (aimStartTime - Time.time) / aimRate));
                }

                //Turn on scope if it's time
                if (scoped && selected && Time.time >= scopeTime && !inScope)
                {
                    inScope = true;
                    var go = GetComponentsInChildren <Renderer>();
                    foreach (Renderer g in go)
                    {
                        if (g.gameObject.name != "Sparks")
                        {
                            g.enabled = false;
                        }
                    }
                    cmra.GetComponent <Camera>().fieldOfView = PlayerWeapons.fieldOfView / zoomFactor;
                }

                //Otherwise if sights zoom then zoom in camera
                if (sightsZoom && selected && !zoomed && !scoped)
                {
                    cmra.GetComponent <Camera>().fieldOfView  = Mathf.Lerp(cmra.GetComponent <Camera>().fieldOfView, PlayerWeapons.fieldOfView / zoomFactor, Mathf.SmoothStep(0, 1, 1 - (aimStartTime - Time.time) / aimRate));
                    wcmra.GetComponent <Camera>().fieldOfView = Mathf.Lerp(wcmra.GetComponent <Camera>().fieldOfView, PlayerWeapons.fieldOfView / zoomFactor, Mathf.SmoothStep(0, 1, 1 - (aimStartTime - Time.time) / aimRate));

                    if (cmra.GetComponent <Camera>().fieldOfView == PlayerWeapons.fieldOfView / zoomFactor)
                    {
                        zoomed = true;
                    }
                }

                //sprinting
            }
            else if (InputDB.GetButton("Sprint") && !InputDB.GetButton("Aim") && canSprint && PlayerWeapons.canSprint && selected && !aiming && CM.grounded && !exhausted && (controller.velocity.magnitude > CM.movement.minSprintSpeed || (/*CM.prone || */ CharacterMotorDB.crouching)))
            {
                sprintNum = Mathf.Clamp(sprintNum - Time.deltaTime, 0, sprintDuration);
                aiming    = false;
                if (!sprinting)
                {
                    aimStartTime = Time.time + sprintRate;
                    curVect      = sprintPosition - transform.localPosition;
                    sprinting    = true;
                    player.BroadcastMessage("Sprinting", SendMessageOptions.DontRequireReceiver);
                    canSwitchWeaponAim = false;
                    startPosition      = transform.localPosition;
                    startRotation      = transform.localEulerAngles;
                }

                //Align to position
                transform.localPosition = Vector3.Slerp(startPosition, sprintPosition, Mathf.SmoothStep(0, 1, 1 - (aimStartTime - Time.time) / sprintRate));
                GunToRotation(sprintRotation, sprintRate);

                //Check if we're out of sprint
                if (sprintNum <= 0)
                {
                    exhausted     = true;
                    sprintEndTime = Time.time + recoverDelay;
                }

                //returning to normal
            }
            else
            {
                if ((aiming || sprinting || switching))
                {
                    if (sprinting)
                    {
                        sprintEndTime = Time.time + recoverDelay;
                        player.BroadcastMessage("StopSprinting", SendMessageOptions.DontRequireReceiver);
                    }
                    switching          = false;
                    aimStartTime       = Time.time + retRate;
                    startPosition      = transform.localPosition;
                    startRotation      = transform.localEulerAngles;
                    sprinting          = false;
                    canSwitchWeaponAim = true;
                    curVect            = hipPosition - transform.localPosition;

                    SendMessageUpwards("NormalSpeed", SendMessageOptions.DontRequireReceiver);
                    if (aiming)
                    {
                        aiming = false;
                        player.BroadcastMessage("StopAiming", SendMessageOptions.DontRequireReceiver);
                    }
                }

                //Align to position
                transform.localPosition = Vector3.Slerp(startPosition, hipPosition, Mathf.SmoothStep(0, 1, 1 - (aimStartTime - Time.time) / retRate));
                GunToRotation(hipRotation, retRate);
            }
            staticAiming    = aiming;
            sprintingPublic = sprinting;
        }
예제 #6
0
        void Update()
        {
            weaponsInactive = (PlayerWeapons.PW.weapons[PlayerWeapons.PW.selectedWeapon] == null);
            if (!weaponsInactive)
            {
                weaponsInactive = (PlayerWeapons.PW.weapons[PlayerWeapons.PW.selectedWeapon].GetComponent <GunScript>().gunActive == false);
            }
            if (!weaponsInactive)
            {
                return;
            }

            //Replenish Sprint time
            float tempSprintTime = 0;

            if (PlayerWeapons.controller.velocity.magnitude == 0)
            {
                tempSprintTime = sprintEndTime;
            }
            if (AimMode.sprintNum < values.sprintDuration && !AimMode.sprintingPublic && Time.time > tempSprintTime)
            {
                if (PlayerWeapons.controller.velocity.magnitude == 0)
                {
                    AimMode.sprintNum = Mathf.Clamp(AimMode.sprintNum + values.sprintAddStand * Time.deltaTime, 0, values.sprintDuration);
                }
                else
                {
                    AimMode.sprintNum = Mathf.Clamp(AimMode.sprintNum + values.sprintAddWalk * Time.deltaTime, 0, values.sprintDuration);
                }
            }
            if (AimMode.sprintNum > values.sprintMin)
            {
                exhausted = false;
            }

            //Handle sprint
            if (InputDB.GetButton("Sprint") && !InputDB.GetButton("Aim") && PlayerWeapons.canSprint && CM.grounded && !exhausted && (PlayerWeapons.controller.velocity.magnitude > CM.movement.minSprintSpeed || (/*CM.prone || */ CharacterMotorDB.crouching)))
            {
                AimMode.sprintNum = Mathf.Clamp(AimMode.sprintNum - Time.deltaTime, 0, values.sprintDuration);
                if (!AimMode.sprintingPublic)
                {
                    AimMode.sprintingPublic = true;
                    BroadcastMessage("Sprinting", SendMessageOptions.DontRequireReceiver);
                    AimMode.canSwitchWeaponAim = false;
                }

                //Check if we're out of sprint
                if (AimMode.sprintNum <= 0)
                {
                    exhausted     = true;
                    sprintEndTime = Time.time + values.recoverDelay;
                }
            }
            else if (AimMode.sprintingPublic)
            {
                AimMode.sprintingPublic = false;
                BroadcastMessage("StopSprinting", SendMessageOptions.DontRequireReceiver);
                BroadcastMessage("NormalSpeed");
                AimMode.canSwitchWeaponAim = true;
            }
        }
예제 #7
0
        void Update()
        {
            if (paused)
            {
                movement.velocity = Vector3.zero;
                return;
            }

            if ((inputMoveDirection.x != 0 || inputMoveDirection.y != 0 || inputMoveDirection.z != 0) && grounded && PlayerWeapons.canMove)
            {
                if (!walking)
                {
                    BroadcastMessage("Walking", SendMessageOptions.DontRequireReceiver);
                }
                walking = true;
            }
            else
            {
                if (walking)
                {
                    BroadcastMessage("StopWalking", SendMessageOptions.DontRequireReceiver);
                }
                walking = false;
            }

            if (!useFixedUpdate)
            {
                UpdateFunction();
            }

            /*if(weaponCamera.transform.localPosition.y > standardCamHeight){
             *      weaponCamera.transform.localPosition.y = standardCamHeight;
             * } else if(weaponCamera.transform.localPosition.y < crouchingCamHeight){
             *      weaponCamera.transform.localPosition.y = crouchingCamHeight;
             * }*/

            if (grounded)
            {
                if (InputDB.GetButtonUp("Crouch") && PlayerWeapons.canCrouch)
                {
                    if (!proneFrame)
                    {
                        if (!crouching && !prone)
                        {
                            /*if(PlayerWeapons.sprinting && movement.useDive && !diving){
                             *      SetVelocity(transform.forward*25 + Vector3.up*12);
                             *      audio.volume = jumpSoundVolume;
                             *      audio.PlayOneShot(jumpSound);
                             *      diving = true;
                             *      lastCamSpeed = camSpeed;
                             *      crouching = false;
                             *      Prone();
                             *      camSpeed = 1;
                             *      movement.crouchedTime = -1;
                             *      //prone = true;
                             * } else {*/

                            Crouch();
                        }
                        else if (crouching && AboveIsClear())
                        {
                            crouching     = false;
                            stopCrouching = true;
                            NormalSpeed();
                        }
                        else if (prone && AboveIsClearProne())
                        {
                            //crouching = false;
                            //stopProne = true;
                            prone = false;
                            Crouch();
                        }
                    }
                    proneFrame = false;
                }
                else if (InputDB.GetButton("Crouch"))
                {
                    if (movement.crouchedTime < 0)
                    {
                        movement.crouchedTime = Time.time;
                    }
                    if (Time.time > movement.crouchedTime + movement.proneHoldTime && movement.crouchedTime > 0 && !prone)
                    {
                        if (useProne)
                        {
                            Prone();
                            proneFrame = true;
                        }
                        movement.crouchedTime = -1;
                    }
                }
                else
                {
                    movement.crouchedTime = -1;
                }
            }
            Vector3 weaponCameraLocalPos = weaponCamera.transform.localPosition;

            if (crouching && !prone)
            {
                if (weaponCameraLocalPos.y > crouchingCamHeight)
                {
                    weaponCameraLocalPos.y = Mathf.Clamp(weaponCameraLocalPos.y - crouchDeltaHeight * Time.deltaTime * camSpeed, crouchingCamHeight, standardCamHeight);
                }
                else
                {
                    weaponCameraLocalPos.y = Mathf.Clamp(weaponCameraLocalPos.y + crouchDeltaHeight * Time.deltaTime * camSpeed, proneCamHeight, crouchingCamHeight);
                }
            }
            else if (prone)
            {
                if (weaponCameraLocalPos.y > proneCamHeight)
                {
                    weaponCameraLocalPos.y = Mathf.Clamp(weaponCameraLocalPos.y - crouchDeltaHeight * Time.deltaTime * camSpeed, proneCamHeight, weaponCameraLocalPos.y);
                }
                else if (!hitProne)
                {
                    GunLook.jostleAmt = new Vector3(-.075f, -.12f, 0);
                    CamSway.jostleAmt = new Vector3(-.01f, -.03f, 0);
                    hitProne          = true;
                    GetComponent <AudioSource>().volume = proneLandSoundVolume;
                    GetComponent <AudioSource>().PlayOneShot(proneLandSound);
                }
            }
            else
            {
                if (weaponCameraLocalPos.y < standardCamHeight)
                {
                    weaponCameraLocalPos.y = Mathf.Clamp(weaponCameraLocalPos.y + standardCamHeight * Time.deltaTime * camSpeed, proneCamHeight, standardCamHeight);
                }
            }
            weaponCamera.transform.localPosition = weaponCameraLocalPos;
        }
예제 #8
0
        void LateUpdate()
        {
            float maxLean = 0;

            if (InputDB.GetButton("LeanRight") && PlayerWeapons.CM.grounded && !CharacterMotorDB.walking)
            {
                if (!leaning || left)
                {
                    leaning   = true;
                    targetPos = startPos + leanAmount;
                    targetRot = -leanRotate;
                    left      = false;
                    colliding = false;
                }
            }
            else if (InputDB.GetButton("LeanLeft") && PlayerWeapons.CM.grounded && !CharacterMotorDB.walking)
            {
                if (!leaning || !left)
                {
                    leaning   = true;
                    targetPos = startPos - leanAmount;
                    targetRot = leanRotate;
                    left      = true;
                    colliding = false;
                }
            }
            else if (leaning)
            {
                colliding = false;
                leaning   = false;
                targetPos = startPos;
            }

            if (left && leaning)
            {
                maxLean   = Check(-1 * transform.right);
                targetPos = Mathf.Max(startPos - leanAmount, -maxLean + skinWidth);
            }
            else if (leaning)
            {
                maxLean   = Check(transform.right);
                targetPos = Mathf.Min(startPos + leanAmount, maxLean - skinWidth);
            }

            Vector3 localpos = transform.localPosition;

            localpos.x = Mathf.Lerp(transform.localPosition.x, targetPos, Time.deltaTime * leanRate * 4);

            Vector3 localEuler = transform.localEulerAngles;

            localEuler.z = Mathf.LerpAngle(0, targetRot, Mathf.Abs(transform.localPosition.x) / leanAmount);

            //clamp our position if necessary
            if (colliding)
            {
                localpos.x = Mathf.Clamp(transform.localPosition.x, -maxLean, maxLean);
            }

            transform.localPosition    = localpos;
            transform.localEulerAngles = localEuler;
        }