I’m currently stuck on a problem with positioning a 3D object at the edge of the screen, and I could really use some advice. I’ve got the basics down: I can easily convert the edge and center of the camera into 3D space, but here’s where I hit a snag. I need to account for the dimensions of the 3D object and its rotation, which complicates things quite a bit.
I’ve been looking into using the `MeshRenderer` to get the object’s dimensions, and while that works in theory, I’m having trouble using that info correctly to position the object. The code I’m working with aims to calculate the screen dimensions of the object, but it doesn’t seem to produce the right results. Here’s the method I wrote:
“`csharp
private Vector2 getScreenDimensions(GameObject g)
{
Vector3 maxSize = g.GetComponent
Vector3 minSize = g.GetComponent
Vector3 screenMin = camera.WorldToScreenPoint(minSize);
Vector2 screenMax = camera.WorldToScreenPoint(maxSize);
float screenWidth = screenMax.x – screenMin.x;
float screenHeight = screenMax.y – screenMin.y;
return new Vector2(screenWidth, screenHeight);
}
“`
I’ve recently found that I can get the vertices of the object through its `BoxCollider`, which lets me calculate the positions of the corners in 3D space. It’s great that I can find those corners, but I’m puzzled about how to determine their actual screen positions after rotation. The theoretical “Top Left” corner that I could assume based on the axis could very well be different after a rotation occurs.
I thought about finding the farthest points from the center to determine which vertices correspond to the actual screen edges. But then I started wondering, wouldn’t this just give me the outlines of the box in 2D without the correct orientation?
I even considered throwing a Raycast at an upright object to check if it hits, thus letting me know if it’s rotated sideways, but this feels a bit convoluted. Alternatively, I’ve been manually placing the objects on the screen just for testing, but is that even a good approach? I’m worried that it might lead to issues down the line, especially with different platforms or resolutions.
Has anyone faced this challenge, or do you have any tips or solutions that could save me from reinventing the wheel here?
It sounds like you’re facing a tricky challenge! Positioning 3D objects at the edge of the screen while correctly accounting for their dimensions and rotation can definitely be complex.
Using the `MeshRenderer` is a solid approach for getting dimensions, but as you noted, it can be misleading due to how it represents the bounding box in world coordinates. Instead, you might want to consider using the vertices of the object’s mesh directly. Here’s a loose guideline:
MeshFilter.sharedMesh.vertices
to get the object’s vertices.transform.localToWorldMatrix
.Camera.WorldToScreenPoint
.As for the rotation consideration, since you’re already accessing the vertices, their positions in screen space will reflect the actual orientation, so you’ll be good there.
If you find that manually positioning objects is becoming cumbersome, you might consider creating a utility function that automates placing the object based on its calculated screen position. This can save you time during development and help avoid resolution-related issues later.
Overall, don’t hesitate to experiment! Finding a solution through trial and error is a great way to learn. Best of luck!
To position a 3D object at the edge of the screen while accounting for its dimensions and rotation, you might want to use a combination of its bounding box and camera perspective to accurately calculate the screen position. The issue you’re encountering with the screen dimensions may originate from the use of the bounds min and max vectors without properly transforming them based on the object’s rotation. Instead of relying solely on the MeshRenderer, consider using the object’s vertices from the BoxCollider, as this will provide a more accurate representation of its orientation. After obtaining the vertices, transform each vertex into screen space using the camera’s WorldToScreenPoint method. This way, you can gather the correct screen positions for all corners, allowing you to determine the adjusted bounds on the 2D screen.
Regarding your concern about the top-left corner not aligning due to rotation, once you have the screen positions of all four corners, you can find the minimum and maximum values of the x and y coordinates. This will give you the actual screen rectangle occupied by your 3D object, including any rotation effects. The idea of using raycasting to check the object’s orientation can indeed be convoluted and might introduce more complexity than necessary. Manually placing the objects can be useful for initial testing, but it is advisable to implement a dynamic positioning method as described above for better adaptability across different platforms and resolutions. With this approach, you’ll leverage both the accurate dimensions and real-time transformations, ensuring that your 3D object is placed correctly at the screen edge regardless of its orientation.