Search Unity

Question Finding Position on a Canvas

Discussion in 'UGUI & TextMesh Pro' started by CladInRed, Apr 21, 2023.

  1. CladInRed

    CladInRed

    Joined:
    Mar 3, 2020
    Posts:
    2
    We use a mounted gun with a stagnate third-person camera for my ship warfare game. However, now tasked with implementing an aiming reticle I am having trouble getting the position right on my Canvas. I've been trying to implement a ray cast from the gun to a camera-space canvas and using the hit position for the reticle.

    Does anyone have any suggestions? image.png

    I am currently rotating the gun through a simple message-based system and clamping before applying, code below

    Code (CSharp):
    1.     private void OnLook(InputValue value)
    2.     {
    3.         _rotateValue = value.Get<Vector2>();
    4.         ApplyRotation();
    5.     }
    6.  
    7.     private void ApplyRotation()
    8.     {
    9.         var xLook= _lookRotation.x - (_rotateValue.y * mouseSensitivity * Time.deltaTime);
    10.         var yLook = _lookRotation.y + (_rotateValue.x * mouseSensitivity * Time.deltaTime);
    11.      
    12.         _lookRotation.x = Mathf.Clamp(xLook,-xClamp, 0);
    13.         _lookRotation.y = Mathf.Clamp(yLook, -yClamp, yClamp);
    14.  
    15.         rotateObject.rotation = Quaternion.Euler(_lookRotation);
    16.         onObjectRotation?.Invoke(_lookRotation);
    17.     }
    I am afraid I'll have to change it up to focus on the reticle and having to rotate the gun towards it, but wanting to prevent extra work ^^'
     

    Attached Files: