Search Unity

Question Raycasting from an object on top of a RawImage(RenderTexture) to the world.

Discussion in 'Scripting' started by Creiz, Feb 8, 2024.

  1. Creiz

    Creiz

    Joined:
    Jun 6, 2017
    Posts:
    130
    Any gurus in here? Please?

    So this is what I have, now.

    Unity_rTaCPk4tO7.png
    The bullseye at the bottom is a "joystick", it controls the crosshair at the top (squint and you'll see it). That crosshair sits on top of a RawImage that renders a RenderTexture. The cube has its collider and everything. I want to raycast from the position of the crosshair inside the image straight back, but I can't figure it out...

    Unity_e04P0n33v9.png
    The code I have currently gives me something like this, pointing down. And when I move the joystick, it disappears completely.

    Here's what I'm working with:
    Code (CSharp):
    1.  
    2. private Ray CalculateRayFromCrosshair()
    3.         {
    4.             // Get the position of the crosshair in the RawImage
    5.            
    6.             Vector3 pos = this.GetComponent<RectTransform>().anchoredPosition;
    7.             // Get the viewport position of the crosshair within the RawImage
    8.             Vector2 viewportPosition = mainCamera.ScreenToViewportPoint(mainCamera.ScreenToWorldPoint(rt.TransformPoint(pos)));
    9.  
    10.             // Convert normalized position to ray in render texture
    11.             Ray ray = mainCamera.ViewportPointToRay(new Vector3(viewportPosition.x, viewportPosition.y, 10f));
    12.  
    13.             return ray;
    14.  
    15.         }
    16.         /// <summary>
    17.         /// Returns the GameObject that is currently targeted.
    18.         /// </summary>
    19.         /// <returns>The GameObject that is currently targeted.</returns>
    20.         public GameObject GetTarget()
    21.         {
    22.            
    23.             Ray ray = CalculateRayFromCrosshair();
    24.             // Shoot a Raycast from the center of the screen.
    25.             RaycastHit hit;
    26.            
    27.             Debug.DrawRay(ray.origin, ray.direction * 10f, Color.red);
    28.             //Vector3 rayDirection = new Vector3(rt.rect.width / 2, rt.rect.height / 2, 0);
    29.             //Vector3 rayDirection = new Vector3(Screen.width / 2, Screen.height / 2, 0);
    30.             Vector3 rayDirection = this.transform.position;
    31.             // If the rayCast hits within the range, return the target.
    32.             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    33.             {
    34.                 targetedObject = hit.transform.gameObject;
    35.                 return targetedObject;
    36.             }
    37.             else
    38.             {
    39.                 targetedObject = null;
    40.                 return null;
    41.             }
    42.            
    43.             Debug.Log(ray);
    44.             Debug.Log(targetedObject);
    45.         }
    46.