Sword Rotation Around the Player

I implemented the sword rotation a little differently. I tried to take the mouse position relative to the player instead of the center of the screen. This way as the player moves around the sword rotation will follow the mouse’s position relative to the player. This also seemed to help with the flipping as it created negative angles for the left side of screen.

private void MouseFollowWithOffset()
    {
        float angle = Mathf.Atan2(Input.mousePosition.y - 
               Camera.main.WorldToScreenPoint(playerController.transform.position).y,

                                     Input.mousePosition.x - 
              Camera.main.WorldToScreenPoint(playerController.transform.position).x) * Mathf.Rad2Deg;

        if(Input.mousePosition.x > 
              Camera.main.WorldToScreenPoint(playerController.transform.position).x)
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0, 0, angle);
            weaponCollider.transform.rotation = Quaternion.Euler(0, 0, angle);
        }
        else
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0, 0, angle);
            weaponCollider.transform.rotation = Quaternion.Euler(0, 0, angle);
        }
    }
2 Likes

It took me so much time to figure this out its unbelievable :smiley:

I wanted it to work the same way as you describe it, but I wanted to keep -180 on y rotation. Without it, it messed up the animation (at least for me…) - it indeed gives good effect, but its not the same as it is in lecture :wink:

The way I made it work is quite similar, but I have adjusted some values:

private void MouseFollowWithOffsett()
    {
        Vector2 mousePosition = playerControler.MousePosition;
        Vector2 playerPosition = playerControler.PlayerScreenPosition;
        Vector2 lookTowards =  mousePosition - playerPosition;
       
        float angle = Mathf.Atan2(lookTowards.y, lookTowards.x) * Mathf.Rad2Deg;
       
        if (mousePosition.x >= playerPosition.x)
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0f, -0, angle);
        }
        else
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0f, -180, -angle - 180);
        }

        
    }
1 Like

currently unsure if this will have consequences down the line, but this added to the PlayerController script seemed to have handled this before the challenge even came up in this video.

 private void Update()
    {
        PlayerInput(); //from earlier in the lesson
        AdjustPlayerFacingDirection();
    }

private void AdjustPlayerFacingDirection()
    {
        if (Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue()).x < transform.position.x)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
    }

1 Like

and of course if I want to follow along with the mouse following it came back to bit me :smiley:

1 Like

Privacy & Terms