Search Unity

Resolved Convert position from one camera's perspective to another?

Discussion in 'General Graphics' started by Kyle45, Jan 16, 2023.

  1. Kyle45

    Kyle45

    Joined:
    Apr 18, 2015
    Posts:
    10
    Hi,

    I have two cameras where one is responsible for all the world objects (MainCam), and the other is responsible for the first-person objects (FPCam). The MainCam has a different fov from the FPCam, but both cameras have the same position. My problem is that I want to draw a line that starts from a position on a first-person object and ends on a position of a world object using a line renderer. Currently the line renderer is rendered by the MainCam. But depending on the MainCam fov, the start position "moves" away from the first-person object. I am looking for a way to adjust the start position so that when the MainCam projection is applied to the line renderer, the start position is where the first-person object is.

    Thank you for the help!

    LineFov80.png LineFov100.png
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
  3. Kyle45

    Kyle45

    Joined:
    Apr 18, 2015
    Posts:
    10
    Yep, that worked like a charm. Thank you! Sadly, I tried something like this before posting but had it flipped where I was doing:
    Code (CSharp):
    1. private Vector3 ConvertPoint(Vector3 point) {
    2.     return fpCam.ViewportToWorldPoint(mainCam.WorldToViewportPoint(point));
    3. }
    Instead of the correct thing:
    Code (CSharp):
    1. private Vector3 ConvertPoint(Vector3 point) {
    2.     return mainCam.ViewportToWorldPoint(fpCam.WorldToViewportPoint(point));
    3. }
    And for some reason I thought I tried flipping it but I guess I did not. Anyways, hopefully this can help someone in the future.
     
    ozgurd5 and bgolus like this.