- Home /
How to position 3D-GUI-Mesh on change of aspect ratio?
I have a 3D-GUI wich is attached to a HUD-Camera. The camera moves with the main cam because of lighting. (So, affecting world position could not be the answer.)
The Item should stay at the upper right.
It's attached to the HUD cam @ x0,y0,z7 (center) and should be in the upper left when running.
When I change the parameters and then change the aspect ratio by resizing, it does nothing at all. The item just stays where it is. It jitters a bit, but then it's on its old position. There is no other script attached to the item.
Here's the script.
using UnityEngine;
using System.Collections;
public class D3GUIAspectRatio : MonoBehaviour {
public Vector2 guiPosition=new Vector2(0.8f,0.1f);
public float oldAspect;
private Transform root;
// Use this for initialization
void Start ()
{
oldAspect = Camera.main.aspect;
SetItemPosition();
}
// Update is called once per frame
void Update ()
{
if (Camera.main.aspect != oldAspect)
{
oldAspect = Camera.main.aspect;
SetItemPosition();
}
}
public void SetItemPosition()
{
Vector3 guiPosCalced = new Vector3();
guiPosCalced = GUIUtility.GUIToScreenPoint(guiPosition);
guiPosCalced.z=transform.localPosition.z;
transform.localPosition = guiPosCalced;
}
}
Thanks for your help.
Also, I would like to know, how to affect the position including Z, so it's in the upper right and not just a little bit up and right. ;) Thanks.
Your answer
Follow this Question
Related Questions
Object to always appear same size on HUD 3 Answers
GUIs for Power ups? No idea. 1 Answer
CoolDown Animation on the GUI 1 Answer
2D Arrow pointing at 3D Position 1 Answer
Off Screen direction indicator HUD element / wrestling with Quaternion 1 Answer