Exemplo n.º 1
0
    public void Resume()
    {
        isPaused = false;
        var direction = directionProvider.GetDirection();

        isMoving = direction.magnitude > 0;
        animator.Play(isMoving ? walkAnimation : idleAnimation);
    }
Exemplo n.º 2
0
    public void FixedUpdate()
    {
        var wasGrounded = isGrounded;

        isGrounded = Physics.Raycast(body.transform.position, Vector3.down, groundCheckDistance, groundLayerMask);
        if (wasGrounded && !isGrounded)
        {
            body.isKinematic = false;
        }
        if (!wasGrounded && isGrounded)
        {
            body.isKinematic = true;
        }

        var currentDir = dirProvider.GetDirection();

        if (currentDir.sqrMagnitude == 0 || !isGrounded)
        {
            return;
        }

        var newRotation = Quaternion.RotateTowards(
            body.transform.rotation,
            Quaternion.LookRotation(currentDir, Vector3.up),
            Time.fixedDeltaTime * config.rotationSpeed
            );

        body.MoveRotation(newRotation);

        foreach (var direction in DIRECTION_MAP.Keys)
        {
            var directionVector = DIRECTION_MAP[direction];
            Debug.DrawRay(body.transform.position + Vector3.up * wallCheckHeight, directionVector * wallCheckDistance, Color.yellow);
            var isBlocked = Physics.Raycast(body.transform.position + Vector3.up * wallCheckHeight, directionVector, wallCheckDistance, groundLayerMask);

            if (!isBlocked)
            {
                continue;
            }
            var compVector = DIRECTION_COMP_MAP[direction];
            compVector.Scale(currentDir);
            if (compVector.normalized == directionVector)
            {
                currentDir.Scale(DIRECTION_INVERSE_COMP_MAP[direction]);
            }
        }



        body.MovePosition(body.position + currentDir * config.moveSpeed * Time.fixedDeltaTime);
    }