简体   繁体   中英

Translate position from 3D camera to 2D camera

Currently I have 2 cameras, one for viewing the 3D objects (Perspective), and the other camera to view 2D objects (Orthographic).
(Also, the view of the camera never intercepts each other.)

I am trying to display a 2D object based on the position of a 3D object, like so: 我正在努力实现的目标的图像

What I have is the respective 2D and 3D Camera , as well as the Vector3 position of the 3D GameObject .

What I currently have:

public Vector2 Convert3DPositionTo2DPosition(Camera camera3D, Vector3 position3D, Camera camera2D) {
    var tempPos = camera3D.WorldToViewportPoint(position3D);

    return camera2D.ViewportToWorldPoint(tempPos);
}

The only problem is that the returned position is not completely aligned with the 3D position.
Which results in this kind of results: 预期和实际结果的图像

Also, I have made sure that both the 3D and 2D object have their pivot point are set correctly in the center, but it still does not work as intended.

(I am currently using Unity 2019.1.14f1 )

Edit

TLDR of what I want:
I want a world position that is based on an object in view of the 3D camera which is now in view of the 2D camera and have them look like they are in the same position in the player's point of view (display screen).

What I am going to instantiate in afterwards (using that position) is a non-UI GameObject.
More specifically a particle system.

Check the Viewport Rect settings for both of your Camera s!

在此处输入图片说明

if there is any difference between the two cameras you will always get an offset in the positions.

I tested your solution using

public Camera camera3d;
public Camera camera2d;
public Transform obj3d;
public Transform obj2d;

void Update()
{ 
    obj2d.position = camera2d.ViewportToWorldPoint(camera3d.WorldToViewportPoint(obj3d.position)); 
}

and it just works fine - as long as the Viewport Rect settings match - as you can see here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM