Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity UI Mouse position axis not aligned on interactive UI

Discussion in 'UGUI & TextMesh Pro' started by Gabriel_wi, Sep 27, 2023.

  1. Gabriel_wi

    Gabriel_wi

    Joined:
    Nov 14, 2022
    Posts:
    2
    I'm making a radial aim indicator on my UI canvas, and after taking pieces of various support threads and tutorials online, I finally got it to behave in the way I would like. However, its alignment with my mouse position is slightly off. I figure it has something to do with my math not lining up because I'll admit, I'm in over my head with the math I put together to make this. I'll show you a gif of the end result as well as the code (it's all inside one script) and I would really appreciate if someone could point out the mistake I've made or the solution.

    You can see my cursor/red aim target match the character's facing direction, but my UI circle is a bit off.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class UI_AimReticle : MonoBehaviour
    7. {
    8.     public Image aimRender;
    9.     public Vector2 mouseInput;
    10.     public float positionOffsetx = 150f;
    11.     public float positionOffsety = 150f;
    12.     public Vector2 _centre;
    13.  
    14.     void Awake()
    15.     {
    16.        // float aimHorizontal = Input.GetAxis("Third Axis");
    17.         // float aimVertical = Input.GetAxis("Fourth Axis");
    18.         _centre = Vector2.zero;
    19.        
    20.  
    21.     }
    22.  
    23.     void FixedUpdate()
    24.     {
    25.         if (Input.GetMouseButton(1))
    26.         {
    27.             aimRender.color = new Color(1, 1, 1, 0.67f);
    28.             mouseInput.x = Input.mousePosition.x - (Screen.width / 2f);
    29.             mouseInput.y = Input.mousePosition.y - (Screen.height / 2f);
    30.             mouseInput.Normalize();
    31.             float angle = Mathf.Atan2(-mouseInput.y, mouseInput.x);
    32.             // angle *= 180;
    33.             angle += 90f;
    34.             if (angle < 0)
    35.             {
    36.                 angle += 360;
    37.             }
    38.             var x = Mathf.Sin(angle) * positionOffsetx;
    39.             var y = Mathf.Cos(angle) * positionOffsety;
    40.             RectTransform reticle = aimRender.GetComponent<RectTransform>();
    41.             reticle.anchoredPosition = _centre + new Vector2(x, y);
    42.         }
    43.         else
    44.         {
    45.             aimRender.color = new Color(1, 1, 1, 0);
    46.         }
    47.    
    48.  
    49.         }
    50.  
    51. }
    52.  
    This feels pretty hard to explain or even title, but since my issue mostly involves the RectTransform of a UI image and my mouse position, I figured this was the right board to ask for support. Please forgive me if not.