Saturday, June 25, 2022
HomeGame Developmenttransformation - Unity: remodeling world to canvas goes flawed when digicam is...

transformation – Unity: remodeling world to canvas goes flawed when digicam is shifting


I’m making an attempt to attract a field round a particular goal in my house recreation. I had this working high-quality utilizing regular methods when my canvas was set to Display screen House – Overlay. Nevertheless my setup is that ship fashions are absolutely current and bodily and contained in the cockpit of every one there’s a “CameraRig” remodel that by default the participant digicam attaches to except they’re free trying or shifting across the ship.

With this setup the GUI would draw over the partitions of the ship and it appeared foolish so I modified the canvas sort to Display screen House – Digicam and set a aircraft distance in order that the gui was exterior of the ship. I modified my field calculations to make use of RectTransformUtility to transform the present display screen place to canvas place.

This all works high-quality when the sport is first ran and the ship is sat a solar positioned at 0, 0, 0:
looking at sun after spawn
and it renders appropriately if I free look the digicam round. Nevertheless the second I set a brand new goal and the ship begins rotating and shifting in direction of it one thing goes wonky and goal containers seem in unusual locations, usually on to the aspect or above/beneath so i’ve to free go searching to search out them and sometimes with flawed width/top. Right here is an instance the place the planet im shifting in direction of is centered however the goal reticule is off to the aspect and can finally not be seen except i arduous look left:

bad cursor position
If I then fly again to the solar usually the form of the field will probably be flawed and barely maligned, so I really feel like that is to do with the cameras place and rotation. However I can not management it, its arduous connected to the digicam and managed by unity and the unity strategies I’ve used to calculate the place are alleged to account for digicam place/rotation, proper?

My code (edited to solely present related):

var heart = targetRenderer.bounds.heart;
var extents = targetRenderer.bounds.extents;

var worldCorners = new[] {
    new Vector3( heart.x + extents.x, heart.y + extents.y, 0 ),
    new Vector3( heart.x + extents.x, heart.y + extents.y, 0 ),
    new Vector3( heart.x + extents.x, heart.y - extents.y, 0 ),
    new Vector3( heart.x + extents.x, heart.y - extents.y, 0 ),
    new Vector3( heart.x - extents.x, heart.y + extents.y, 0 ),
    new Vector3( heart.x - extents.x, heart.y + extents.y, 0 ),
    new Vector3( heart.x - extents.x, heart.y - extents.y, 0 ),
    new Vector3( heart.x - extents.x, heart.y - extents.y, 0 ),
};

var screenCorners = worldCorners.Choose(nook => cam.WorldToScreenPoint(nook)).ToArray();

var canvasCorners = screenCorners.Choose(c =>
    {
        c.z = 0;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, c, WorldCanvas.worldCamera, out var transformedPoint);                    
        return transformedPoint;
    }
).ToList();

var screenCorners = worldCorners.Choose(nook => cam.WorldToScreenPoint(nook)).ToArray();

var canvasCorners = screenCorners.Choose(c =>
    {
        c.z = 0;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, c, WorldCanvas.worldCamera, out var transformedPoint);                    
        return transformedPoint;
    }
).ToList();

maxX = canvasCorners.Max(nook => nook.x);
minX = canvasCorners.Min(nook => nook.x);
maxY = canvasCorners.Max(nook => nook.y);
minY = canvasCorners.Min(nook => nook.y);

var width = maxX - minX;
var top = maxY - minY;
var posX = minX;
var posY = minY; 

GuiImage.anchoredPosition = new Vector2(posX, posY);                                                           


GuiImage.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
GuiImage.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, top);

if you need to see full replace technique unedited then here’s a paste:
hastebin
(scaleby is 0, usedeltas is fake).

The digicam the participant is trying via and the digicam utilized by the canvas are the identical digicam.

I’m at an entire loss as to why it’s behaving this manner. Welcome all enter!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments