示例#1
0
    // Update is called once per frame
    void Update()
    {
        Cursor.visible   = false;
        Cursor.lockState = CursorLockMode.Locked;
        if (Input.GetMouseButton(0))
        {
            gunManager.TryToTriggerGun();
        }

        // flaming weapon
        if (Input.GetKeyDown(KeyCode.F))
        {
            gunManager.FlamingWeaponOpen();
        }

        if (Input.GetKeyUp(KeyCode.F))
        {
            gunManager.FlamingWeaponClose();
        }



        //決定鍵盤input的結果
        Vector3 movDirection = Vector3.zero;

        if (Input.GetKey(KeyCode.W))
        {
            movDirection.z += 1;
        }
        if (Input.GetKey(KeyCode.S))
        {
            movDirection.z -= 1;
        }
        if (Input.GetKey(KeyCode.D))
        {
            movDirection.x += 1;
        }
        if (Input.GetKey(KeyCode.A))
        {
            movDirection.x -= 1;
        }
        movDirection = movDirection.normalized;

        //決定要給Animator的動畫參數
        if (movDirection.magnitude == 0 || !JumpSensor.IsCanJump())
        {
            currentSpeed = 0;
        }
        else
        {
            if (movDirection.z < 0)
            {
                currentSpeed = -MoveSpeed;
            }
            else
            {
                currentSpeed = MoveSpeed;
            }
        }
        animatorController.SetFloat("Speed", currentSpeed);

        if (currentSpeed != 0 && !isWalking)
        {
            step.Play();
            isWalking = true;
        }
        if (currentSpeed == 0 && isWalking)
        {
            step.Pause();
            isWalking = false;
        }

        //轉換成世界座標的方向
        Vector3 worldSpaceDirection = movDirection.z * rotateYTransform.transform.forward +
                                      movDirection.x * rotateYTransform.transform.right;
        Vector3 velocity = rigidBody.velocity;

        velocity.x = worldSpaceDirection.x * MoveSpeed;
        velocity.z = worldSpaceDirection.z * MoveSpeed;

        if (Input.GetKey(KeyCode.Space) && JumpSensor.IsCanJump())
        {
            velocity.y = JumpSpeed;
        }

        rigidBody.velocity = velocity;

        //計算滑鼠
        rotateYTransform.transform.localEulerAngles += new Vector3(0, Input.GetAxis("Horizontal"), 0) * rotateSpeed;
        currentRotateX += Input.GetAxis("Vertical") * rotateSpeed;

        if (currentRotateX > 90)
        {
            currentRotateX = 90;
        }
        else if (currentRotateX < -90)
        {
            currentRotateX = -90;
        }
        rotateXTransform.transform.localEulerAngles = new Vector3(-currentRotateX, 0, 0);
    }