Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Assistance with debugging an issue with weapon aiming at mouse pointer

Discussion in '2D' started by MarkElement, Sep 10, 2023.

  1. MarkElement

    MarkElement

    Joined:
    Jan 2, 2023
    Posts:
    12
    I'm working through adding mouse and keyboard support for my 2D game, which is turning into a bit of a nightmare since i initially learned the wrong (or old) input system and ended up having to strip almost everything out to replace with the new input sytem so i'm able to easily detect controller scheme switching.

    Anyway, Thats (kind of) done now but an issue i'm running into is due to the way my character aims (the weapon orbits the player character) When using a gamepad this works perfectly, i've modified my logic to accomodate for mouse input as opposed to analog stick input and again, it "kind of" works.

    Basically, if the mouse gets too close to the character the weapon sprite seems to duplicate and rotate in a strange way. So the question is does anyone have any suggestions / ideas on how i could prevent this?

    I few things i've tried:

    1. Attempting to detect if my mouse is insite the characters CapsuleCollider2D using
    playerRigidBody.OverlapPoint(Input.mousePosition);
    This didn't work, no matter what i did i couldn't get a true from it.
    2. Attempting to manually detect if the mouse is "close" to the character using the various values from within my mouse aiming code but this got overly complex very quickly and started to make me want to stab myself in the eye to releive the brain ache :D

    Here's my mouse aim code:

    This falls inside the
    else 
    end of an
    if(isGamePad)


    Code (CSharp):
    1.  // Flip the weapon into the direction it's aiming
    2. if (WeaponIsFacingRight())
    3. {
    4.      weaponSprite.flipX = true;
    5.      weaponSprite.flipY = false;
    6. }
    7. else
    8. {
    9.      weaponSprite.flipX = true;
    10.      weaponSprite.flipY = true;
    11. }
    12.  
    13.  
    14. // This is where the mouse is in relation to the weapon aim
    15. var pos = Camera.main.WorldToScreenPoint(weaponSlot.transform.position);
    16. var dir = Input.mousePosition - pos;
    17.  
    18. // Get the x and y aim direction, times by the tick rate and player move speed
    19. float x = dir.x * Time.deltaTime * moveSpeed;
    20. float y = dir.y * Time.deltaTime * moveSpeed;
    21.  
    22. rads = Mathf.Atan2(y, x);
    23.  
    24. float degrees = rads * Mathf.Rad2Deg;
    25.  
    26.  
    27. weaponSlot.transform.position = new Vector3(weaponPositionX, weaponPositionY, playerPosition.z); // playerPosition;
    28. weaponSlot.transform.localEulerAngles = new Vector3(0, 0, degrees);

    And a clip showing the behaviour i'm trying to fix:




    I'm sure the solution lays in detecting and action on dir.x dir.y being certain values but i'm struggling to get my head around it based on debugging outputs etc. Any help appreciated!
     
  2. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    142
    this is the problematic line

    Code (CSharp):
    1. var pos = Camera.main.WorldToScreenPoint(weaponSlot.transform.position);
    you compare your mouse to the weapon instead of the character

    because of this when the cursor gets close your weapon changes position and now it will be in a new position relative to the cursor, and will now teleport forever

    change the worldtoscreen to the transform of your char
     
    Kurt-Dekker and MarkElement like this.
  3. MarkElement

    MarkElement

    Joined:
    Jan 2, 2023
    Posts:
    12

    Perfect! Thank you!