Пример #1
0
    private void FixedUpdate()
    {
        // Get input from movement and get relative mouse position to player position
        moveHorizontal = Input.GetAxisRaw("Horizontal");
        moveVertical   = Input.GetAxisRaw("Vertical");
        relMousePos    = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
        relMousePos    = new Vector3(relMousePos.x, relMousePos.y, 0.0f).normalized;

        // Set hand position
        hand.transform.position = transform.position + new Vector3(relMousePos.x, HAND_HEIGHT, relMousePos.y) * HAND_DISTANCE;

        // Set sword rotation
        sword.SetRotation(Vector3.SignedAngle(relMousePos, Vector3.right, Vector3.forward));

        // Set hitbox rotation
        hitbox.SetRotation(Vector3.SignedAngle(relMousePos, Vector3.right, Vector3.forward));

        // Calculate movement vector
        velocity = Vector3.zero;
        Vector3 movement = moveVertical * Vector3.forward + moveHorizontal * Vector3.right;

        if (movement.magnitude > 1.0f) // Normalize movement vector
        {
            movement = movement.normalized;
        }

        // Animate player sprite based on inputs
        animator.SetFloat("movement", movement.magnitude);
        animator.SetFloat("facingY", relMousePos.y);
        animator.SetFloat("facingX", relMousePos.x);

        // Actually move player if movement is big enough
        if (movement.magnitude > 0.25f)
        {
            Vector3.Normalize(movement);

            // Pass in relevant information for running animation
            animator.SetFloat("dx", movement.x);
            animator.SetFloat("dy", movement.z);

            // Set new velocity
            velocity = movement.normalized * PLAYER_SPEED;

            // Actually move player
            transform.Translate(movement.normalized * PLAYER_SPEED * Time.fixedDeltaTime, Space.World);
        }
    }