I’m using a simple Cinemachine Virtual Camera which follows the player with a bit of delay to make it smooth. When holding the mouse over an item, a tooltip shows, and I set it’s position like the code below.
When I move, the tooltip lags after, and is not fixed to the item. I have tried to set this in Update, FixedUpdate and LateUpdate.
The wantedPosition is where it should be if it’s in the middle of the screen, then it sets it so it doesn’t go outside the screen. camera1 is Camera.main, which is the main camera where Cinemachine Brain is.
The tooltip is a UI element in a Screen Space - Overlay Canvas. Should I maybe have the tooltip as a real gameobject instead?
Any clues on what I’m doing wrong?
GrandioseRaggedAracari
// Run in Update, FixedUpdate or LateUpdate (same results)
void SetTooltipPositionItem() {
var wantedPosition = BlueGoo.Utils.Utils.WorldToUISpace(camera1, canvas, currentPosition) + (Vector3) tooltipOffset;
gameObject.transform.position = BlueGoo.Utils.Utils.CheckIfRectIsInsideParent(wantedPosition, rect, parentRect);
}
public static Vector3 WorldToUISpace(Camera camera, Canvas parentCanvas, Vector3 worldPos) {
//Convert the world for screen point so that it can be used with ScreenPointToLocalPointInRectangle function
Vector3 screenPos = camera.WorldToScreenPoint(worldPos);
Vector2 movePos;
//Convert the screenpoint to ui rectangle local point
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, screenPos, parentCanvas.worldCamera, out movePos);
//Convert the local point to world point
return parentCanvas.transform.TransformPoint(movePos);
}
public static Vector3 CheckIfRectIsInsideParent(Vector3 wantedPosition, RectTransform rect, RectTransform parent, float padding = 20) {
// Set position first, so it can check if it's inside
rect.position = wantedPosition;
Vector3[] corners = new Vector3[4];
Vector3[] parentCorners = new Vector3[4];
Vector2 diff = Vector2.zero;
// Get corners
rect.GetWorldCorners(corners);
parent.GetWorldCorners(parentCorners);
// Corner 0 = bottom left, 2 = top right
// If tooltip is outside, set diff
if (corners[0].x < parentCorners[0].x + padding) {
diff.x = parentCorners[0].x - corners[0].x + padding; // Utanför till vänster
} else if (corners[2].x > parentCorners[2].x - padding) {
diff.x = parentCorners[2].x - corners[2].x - padding; // Utanför till höger
}
if (corners[1].y > parentCorners[2].y - padding) {
diff.y = parentCorners[2].y - corners[1].y - padding; // Utanför uppåt
} else if (corners[0].y < parentCorners[0].y + padding) {
diff.y = parentCorners[0].y - corners[0].y + padding; // Utanför neråt
}
return wantedPosition + (Vector3) diff;
}