Hi all,
I have this on Unity Answers so sorry for cross-posting but I could really use some help with this.
I’m using the following code to create a wayfinder in my game interface that rotates to indicate the direction of the next objective. Works perfectly.
// texture for the wayfinder
var compTexture: Texture2D = null;
// positioning inset from top and right edges of the screen
var compInsetX : int = 150;
var compInsetY : int = 100;
// in-world point (empty GO) to allow us to calculate heading
var pointer;
// the location of the current objective
var wp;
// player character GO
var PC : GameObject;
// angle to the current objective, matrix to hold the GUI object,
// pivot point of the wayfinder
// and rectangle co-ordinates to draw on screen
private var heading ;
private var matrixBackup:Matrix4x4;
private var compPos:Vector2 ;
private var compRect:Rect;
function Start(){
// PC = GameObject.Find("wp_pch_01");
pointer = GameObject.Find("objective_pointer");
wp = GameObject.Find("objective1");
}
function Update () {
// point the empty GO at the position current objective
pointer.transform.LookAt(wp.transform.position);
// store a reference to the heading calculated by subtracting the rotation
// angle of the player
// from that of the pointer
heading = (pointer.transform.eulerAngles.y) - (PC.transform.eulerAngles.y);
}
function OnGUI () {
// This function handles drawing the GUI object on screen
// store the width of the wayfinder texture and height
var compassWidth : int = compTexture.width;
var compassHeight : int = compTexture.height;
// get the position of texture on screen by subtracting the X inset value
// from the left edge of the screen and subtracting half the texture width
// from this
var ScreenX : int = ((Screen.width - compInsetX) - (compassWidth * 0.5));
var ScreenY : int = compInsetY;
// a matrix to contain the GUI object to be rotated
var matrixBackup:Matrix4x4 = GUI.matrix;
// the pivot point of the graphic
var compPos:Vector2 = Vector2(ScreenX+(compassWidth*0.5),
ScreenY+(compassHeight*0.5));
// rotate the graphic around its pivot and towards the heading
GUIUtility.RotateAroundPivot(heading, compPos);
// rectangle co-ordinates to draw on screen
var compRect:Rect = Rect(ScreenX,ScreenY,compassWidth,compassHeight);
// draw the wayfinder texture in the rectangle defined
GUI.DrawTexture(compRect, compTexture);
// end of the matrix
GUI.matrix = matrixBackup;
}
I’m now trying to implement the resolution independence code from the 3D Platformer tutorial so that the game interface graphics will rescale for different resolutions. The graphics are designed for a 1024 x 768 resolution and adding the lines
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3(Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
var scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width;
where nativeVerticalResolution is set to 1024, and scaledResolutionWidth replaces Screen.width in my previous code, successfully rescales the other graphic in the interface, a health indicator (in a different script).
However, with the wayfinder, it’s pivot point is wrong and I get a null reference exception error, regardless of which resolution I pick in the Game window. Actually, I get the same error when changing resolutions without the resolution independence code.
I found a post on here regarding problems with the mouse position where the answer was that the mouse’s co-ordinates didn’t match those of the new screen resolution, and the following lines were provided as a solution. Is this appropriate in my case?
var transMouse = GUI.matrix.inverse.MultiplyPoint3x4(Vector3(ix, Screen.height - iy, 1));
GUI.Button(Rect(transMouse.x, transMouse.y, 32, 32), "");
Finally, by scaling the graphics using this method, will they be stretched up if the resolution is greater than 1024 x 768? If so, does anyone know how I can avoid that.
Many thanks.