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 Finding world point of second cameras view of canvas

Discussion in 'Scripting' started by rhyswittingslow, Aug 18, 2023.

  1. rhyswittingslow

    rhyswittingslow

    Joined:
    Feb 24, 2016
    Posts:
    11
    Ok, so I'm well out of my depth here. But ill try explain it the best that I can.

    I've got the main camera (player) looking at a computer screen in game, this screen has a canvas on it and a render texture from a secondary camera that viewing the scene from a top down view.

    What id like to do is have an object in the world space follow exactly to where the "in canvas" curser o, the in world screen is. Basically having a raycast from that location in world space so I can see what the curser is hovering over.

    As you can see on the images, the screen view is showing the cursor in the middle of the
    red box, however in the other image you can see the blue sphere is in a slightly different location.
    1.png

    2.png
    This is the code i have, however I'm completely sure it doesn't work correctly.
    Code (CSharp):
    1. Vector2 mouseScreenPosition = Mouse.current.position.ReadValue();
    2.                 screenPositon = mouseScreenPosition;
    3.                 screenPositon.z = birdsEyeCam.nearClipPlane;
    4.                 worldPosition = birdsEyeCam.ScreenToWorldPoint(screenPositon);
    5.                 rayObj.transform.position = worldPosition;
    Literally no idea how to even ask this question correctly.
    Any help would be muchly appreciated.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
  3. rhyswittingslow

    rhyswittingslow

    Joined:
    Feb 24, 2016
    Posts:
    11
    I'm not entirely sure how to implement this Plane.Raycast code into my script. How would I necessarily position the ray object to the same position on the in-world computer screen using this?
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I think technically you're asking how a mini map works, and how you can click on the mini map to move the camera to that position in the world. I'd look into a couple of those types of tutorials, to get some ideas. :)
     
  5. rhyswittingslow

    rhyswittingslow

    Joined:
    Feb 24, 2016
    Posts:
    11
    Oh true! That actually is a great example. I'll give them a look over!