Esempio n. 1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        _animator.speed = _rgd2d.velocity.magnitude < 0.05f ? 0 : 1;
        if (ladder_mode)
        {
            ladderMode();
            return;
        }

        var contactsCount = _rgd2d.GetContacts(contact_list);

        is_grounded = (contact_list.Take(contactsCount).Any(contact => contact.normal.y > 0.9f));

        if (is_grounded && pressed_fall)
        {
            _animator.SetTrigger("Jump");
            _rgd2d.velocity = Vector2.zero;
            Debug.Log("FREEFALLING");
            _boxCollider2D.isTrigger = true;
            is_dropping = true;
        }
        else if (is_grounded)
        {
            _animator.SetTrigger("Contact");
        }

        Vector2 velocity;
        var     newSpeed    = is_dropping ? 0 : currentSpeed;
        var     newVelocity = new Vector2(Input.GetAxisRaw("Horizontal") * newSpeed, (velocity = this._rgd2d.velocity).y);

        _rgd2d.velocity = Vector2.SmoothDamp(velocity, newVelocity, ref cur_velo, m_smooth_time);

        var absX = Math.Abs(velocity.x);

        if (absX > .1f)
        {
            _spriteRenderer.flipX = velocity.x > 0;
        }

        if (!ladder_mode)
        {
            _animator.SetBool("Moving", !is_dropping && absX > 0.1f);
        }

        if (is_dropping)
        {
            return;
        }

        if (is_grounded && pressed_jump)
        {
            _animator.SetTrigger("Jump");
            is_grounded = false;
            _rgd2d.AddForce(new Vector2(0f, m_jump_velocity * 50));
            Debug.Log("PRESSED JUMP");
        }


        if (pressed_repair)
        {
            StartCoroutine(PlayerIsFixing());
        }

        pressed_jump = false;
        pressed_fall = false;
        animTrigger  = null;

        // ShowArrow();
    }
Esempio n. 2
0
    // Update is called once per frame
    void Update()
    {
        // Calculate smoothed movement vector
        if (_controller.isGrounded && !_isResting) // Player on ground
        {
            _movement = Vector2.SmoothDamp(
                _movement,
                _input,
                ref _movementSmoothVelocity,
                1 / acceleration * Time.deltaTime
                );
        }
        else if (_isResting) // Player resting
        {
            _movement = Vector2.SmoothDamp(
                _movement,
                Vector2.zero,
                ref _movementSmoothVelocity,
                1 / acceleration * Time.deltaTime
                );
        }
        else // Player in air
        {
            _movement = Vector2.SmoothDamp(
                _movement,
                _movement + ((_input - _movement) * airControl),
                ref _movementSmoothVelocity,
                1 / acceleration * Time.deltaTime
                );
        }

        // Calculate smoothed sprint amount and camera angle
        _sprintAmount = Mathf.Lerp(_sprintAmount, _isSprinting ? 1 : 0, acceleration);
        _camAngle     = Quaternion.Slerp(_camAngle, cam.rotation, 1 / acceleration * Time.deltaTime);

        // Calculate movement direction from angle and input
        Vector3 direction = new Vector3(_movement.x, 0f, _movement.y);

        if (direction.magnitude >= 0.1f)
        {
            float targetAngle = (Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg)
                                + _camAngle.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(
                transform.eulerAngles.y,
                targetAngle,
                ref _turnSmoothVelocity,
                1 / acceleration * Time.deltaTime
                );
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDirection = transform.rotation
                                    * Vector3.forward
                                    * direction.magnitude;
            direction = moveDirection;
        }

        // Calculate speed depending on sprint input
        float moveSpeed = Mathf.Lerp(speed, sprintSpeed, _sprintAmount);

        direction *= (moveSpeed * Time.deltaTime);

        // Add gravity
        ApplyGravity();
        direction.y = _verticalVelocity * Time.deltaTime;

        // Move
        _controller.Move(direction);

        // Calculate isGrounded
        if (_controller.isGrounded)
        {
            _lastGrounded = Time.time;
        }
        _isGrounded = Time.time - _lastGrounded < fallTimeThreshold;
    }
Esempio n. 3
0
        public static Vector2 SmoothDamp(Vector2 current, Vector2 target, ref Vector2 currentVelocity, float smoothTime, float maxSpeed)
        {
            float deltaTime = Time.deltaTime;

            return(Vector2.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime));
        }