Esempio n. 1
0
 void Update()
 {
     if (!UISystem.Instance.CutSceneDisplaying())
     {
         startedUsing = _input.GetKeyDown(KeyCode.Space);
         stoppedUsing = _input.GetKeyUp(KeyCode.Space);
         isUsing      = _input.GetKey(KeyCode.Space);
     }
 }
    // the Update loop contains a very simple example of moving the character around and controlling the animation
    void FixedUpdate()
    {
        if (_controller.isGrounded)
        {
            _velocity.y = 0;
        }

        if (_input.GetKey(KeyCode.RightArrow))
        {
            normalizedHorizontalSpeed = 1;
            if (transform.localScale.x < 0f)
            {
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
            }

            if (_controller.isGrounded)
            {
                _animator.Play(Animator.StringToHash(RunState));
            }
        }
        else if (_input.GetKey(KeyCode.LeftArrow))
        {
            normalizedHorizontalSpeed = -1;
            if (transform.localScale.x > 0f)
            {
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
            }

            if (_controller.isGrounded)
            {
                _animator.Play(Animator.StringToHash(RunState));
            }
        }
        else
        {
            normalizedHorizontalSpeed = 0;

            if (_controller.isGrounded)
            {
                _animator.Play(Animator.StringToHash(IdleState));
            }
        }


        // we can only jump whilst grounded
        if (_controller.isGrounded && _input.GetKeyDown(KeyCode.UpArrow))
        {
            _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
            _animator.Play(Animator.StringToHash(JumpState));
        }


        // apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control
        var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping;         // how fast do we change direction?

        _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);

        // apply gravity before moving
        _velocity.y += gravity * Time.deltaTime;

        // if holding down bump up our movement amount and turn off one way platform detection for a frame.
        // this lets us jump down through one way platforms
        if (_input.GetKey(KeyCode.DownArrow))
        {
            if (_controller.isGrounded)
            {
                _velocity.y *= 3f;
            }
            _controller.ignoreOneWayPlatformsThisFrame = true;
        }

        _controller.move(_velocity * Time.deltaTime);

        // grab our current _velocity to use as a base for all calculations
        _velocity = _controller.velocity;
    }