- Home /
Translating gui.matrix to java problem
Hello everyone
I have this script of gui.matrix and it works great but its for c++ and i did my whole project in java and i cant just change it so i ask if someone knows how to translate the script i really tried everything but it just doesnt do anything
Script:
Matrix4x4 transformation = Matrix4x4.TRS(
Vector3.zero,
Quaternion.identity,
Vector3.one);
void OnGUI() {
// We need to assign the GUI matrix before GUI controls are shown.
GUI.matrix = transformation;
if (GUI.Button(new Rect((Screen.width - 250) / 2, (Screen.height - 100) / 2 - 55, 250, 100), "TRANSFORM GUI")) {
transformation = Matrix4x4.TRS(
new Vector3(-Screen.width * 0.2f, -Screen.height * 0.7f, 0), // move the GUI a little to fit the transformed controls in the screen
Quaternion.Euler(0, 0, 15), // rotate 15 degrees clockwise around Z, because controls are in XY plane
new Vector3(2, 2, 1)); // scale GUI x2 times in X and Y
}
if (GUI.Button(new Rect((Screen.width - 250) / 2, (Screen.height - 100) / 2 + 55, 250, 100), "NORMALIZE GUI")) {
transformation = Matrix4x4.TRS(
Vector3.zero,
Quaternion.identity,
Vector3.one);
}
}
script made by unimechanic
Thanks in advance Skullbeats1
Answer by Bunny83 · Oct 03, 2014 at 02:20 AM
First of all this script is written in C# and not C++ also you are using UnityScript and not Java. The syntax of the relevant parts is exactly the same, you just need to change the declaration of the variable and the method. Also a matrix with no translation, no rotation and a scale of 1.0 is the identity matrix, so you can simply exchange it with the Matrix4x4.identity constant
var transformation = Matrix4x4.identity;
function OnGUI() {
// We need to assign the GUI matrix before GUI controls are shown.
GUI.matrix = transformation;
if (GUI.Button(Rect((Screen.width - 250) / 2, (Screen.height - 100) / 2 - 55, 250, 100), "TRANSFORM GUI")) {
transformation = Matrix4x4.TRS(
Vector3(-Screen.width * 0.2f, -Screen.height * 0.7f, 0), // move the GUI a little to fit the transformed controls in the screen
Quaternion.Euler(0, 0, 15), // rotate 15 degrees clockwise around Z, because controls are in XY plane
Vector3(2, 2, 1)); // scale GUI x2 times in X and Y
}
if (GUI.Button(Rect((Screen.width - 250) / 2, (Screen.height - 100) / 2 + 55, 250, 100), "NORMALIZE GUI")) {
transformation = Matrix4x4.identity;
}
}
but i have still this error
Ignoring invalid matrix assinged to GUI.matrix - the matrix needs to be invertible. Did you scale by 0 on Z-axis? UnityEngine.GUI:set_matrix($$anonymous$$atrix4x4) GUI $$anonymous$$AN:OnGUI() (at Assets/SCRIPTS/GUI $$anonymous$$AN.js:20)
I just tested the script as it is and it works without any errors. You probably have somehow set the matrix in the inspector to the wrong values. The easiest fix is to make your variable private.
Alternatively you can check the values of the matrix in the inspector. All values should read 0 except E00, E11, E22 and E33 which have to be 1. In your case one or more values of the main diagonal (E00, E11, E22, E33) is probably 0.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to move the object at the center of the grid cell? 0 Answers
Pass Matrix4x4 to ComputeShader? 1 Answer