Пример #1
0
    void Update()
    {
        //if the user runs off the edge of the map, reset position, (if this is game mode, lose a life or add time to total time)
        if (transform.position.y < -50)
        {
            transform.position = new Vector3(startX, startY, startZ);
        }

        float usersRunSpeed = (float)SavedSettings.RunSpeed;

        runSpeed = usersRunSpeed;

        //if we are in game mode, check if we've yet finished the game (so we dont keep calling finish)
        //if we haven't and the game is over, finish the game
        if (SavedSettings.GameMode == true)
        {
            if (haveSentFinish == false)
            {
                if (checkGameOver())
                {
                    canMove = false;
                    timer.finishTimer();
                    haveSentFinish = true;
                    return;
                }
            }


            //the game pauses when the user hits a patrol or mobile phone
            if (gamePaused == true)
            {
                if (Input.GetKeyDown(KeyCode.G))
                {
                    gamePaused = false;
                    resumeGame();
                    transform.position = new Vector3(startX, startY, startZ);
                    if (drunk == true)
                    {
                        blurrPanl.SetActive(true);
                    }
                }
            }

            //if they're drunk, the screen is blurred for 10 seconds
            if (drunk == true)
            {
                tenSec -= Time.smoothDeltaTime;
                if (tenSec <= 0)
                {
                    drunk = false;
                    blurrPanl.SetActive(false);
                }
            }
        }


        //if we can move the player (i.e. the game isn't over or any other blocker)
        if (canMove == true)
        {
            //force controller down slope. Disable jumping
            if (myAng > 50)
            {
                canJump = false;
            }
            else
            {
                canJump = true;
            }

            //if the user isnt jumping
            if (grounded)
            {
                isJumping = false;

                //if the user is in first person camera mode
                if (camera1.transform.gameObject.transform.GetComponent <UserCamera>().inFirstPerson == true)
                {
                    moveDirection = new Vector3((Input.GetMouseButton(0) ? Input.GetAxis("Horizontal") : 0), 0, Input.GetAxis("Vertical"));
                }

                moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0), 0, Input.GetAxis("Vertical"));

                moveDirection  = transform.TransformDirection(moveDirection);
                moveDirection *= isWalking ? walkSpeed : runSpeed;

                moveStatus = "idle";



                if (moveDirection != Vector3.zero)
                {
                    moveStatus = isWalking ? "walking" : "running";
                }

                //jump if user presses space and isnt already jumping (i.e is grounded)
                if (Input.GetKeyDown(KeyCode.Space) && canJump)
                {
                    moveDirection.y = jumpSpeed;
                    isJumping       = true;
                }
            }


            // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
            if (camera1.transform.gameObject.transform.GetComponent <UserCamera>().inFirstPerson == false)
            {
                if (Input.GetMouseButton(1))
                {
                    transform.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);
                }
                else
                {
                    transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
                }
            }
            else
            {
                if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
                {
                    transform.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);
                }
            }

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


            //Move controller
            CollisionFlags flags;
            if (isJumping)
            {
                flags = controller.Move(moveDirection * Time.deltaTime);
            }
            else
            {
                flags = controller.Move((moveDirection + new Vector3(0, -100, 0)) * Time.deltaTime);
            }

            grounded = (flags & CollisionFlags.Below) != 0;
        }
    }