I am trying to get a textured plane object to be always in the center of the screen when being looked at (some sort of overlay/hud).
At the moment I use a billboard shader to keep it rotated to the camera
o.pos = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0)) - float4(v.vertex.y, v.vertex.x, 0.0, 0.0) * float4(0.1, 0.1, 1.0, 1.0));
How would I go about getting it to move to the center of the screen/camera when the object is being rendered? Which transformations do I have to do? Is it even possible to do?
keep a reference to your camera… say public Transform camTran;
or get it from Camera.main
then in your billboard code… just point it’s forward vector towards the camera…
transform.forward = (camTran.position - transform.forward.position).normalized;
or
transform.LookAt(camTran.position);
in order to keep it always in front of the camera…
you need the distance you want to maintain from the camera… either calculate current distance first…
float currDistance = (camTran.position - transform.posion).magnitude;
or just keep it in a public float desiredCamDistance = 20f;
then set the position of the object along the camera’s forward looking vector
transform.position = camTran.forward * desiredCamDistance;
if you want to do both… set the position first, then point it at the camera second.