Moving GUITexture

He guys.

I’m working on a GUI texture that needs to move on a click. Now basicly I got the thing moving in a static resolution, but my client needs it to be a dynamic resolution. (Both the trigger script shown here, aswell as the MoveObject script are from the wiki)

What i came up with now (the working script :p)
In this situation the object is just moving fine.

var normalTexture:Texture;
var hoverTexture:Texture;
var pressedTexture:Texture;

private var state = 0;
private var state2 = 0;
private var myGUITexture:GUITexture;

myGUITexture = GetComponent (GUITexture);

function OnMouseEnter(){
	state++;
	if (state == 1) myGUITexture.texture = hoverTexture;
}

function OnMouseDown(){
	state++;
	if (state == 2) myGUITexture.texture = pressedTexture;
}

function OnMouseUp(){
	if (state == 2){
		state--;
		state2++;
		if (state2 == 1){
			yield MoveObject.use.Translation(transform, Vector3(-0.25, 0.875, 0.0), Vector3(0, 0.875, 0.0), 0.25, MoveType.Speed);
		}else{
			yield MoveObject.use.Translation(transform, Vector3(0, 0.875, 0.0), Vector3(-0.25, 0.875, 0.0), 0.25, MoveType.Speed);
			state2 = 0;
		}
	}else{
		state--;
		if (state < 0) state = 0;
	}
	myGUITexture.texture = normalTexture;
}

function OnMouseExit(){
	if (state > 0) state--;
	if (state == 0) myGUITexture.texture = normalTexture;
}

Now to get it working on a dynamic resolution I added a line to calculate the x Postion relative to the resolution and store it in var xlocA. After that i changed the “-0,25” value of the MoveObject call into the var xlocA. The whole thing resulting in this script:

var normalTexture:Texture;
var hoverTexture:Texture;
var pressedTexture:Texture;

private var state = 0;
private var state2 = 0;
private var xlocA:int = (-200/Screen.width);
private var myGUITexture:GUITexture;

myGUITexture = GetComponent (GUITexture);

function OnMouseEnter(){
	state++;
	if (state == 1) myGUITexture.texture = hoverTexture;
}

function OnMouseDown(){
	state++;
	if (state == 2) myGUITexture.texture = pressedTexture;
}

function OnMouseUp(){
	if (state == 2){
		state--;
		state2++;
		if (state2 == 1){
			yield MoveObject.use.Translation(transform, Vector3(xlocA, 0.875, 0.0), Vector3(0, 0.875, 0.0), 0.25, MoveType.Speed);
		}else{
			yield MoveObject.use.Translation(transform, Vector3(0, 0.875, 0.0), Vector3(xlocA, 0.875, 0.0), 0.25, MoveType.Speed);
			state2 = 0;
		}
	}else{
		state--;
		if (state < 0) state = 0;
	}
	myGUITexture.texture = normalTexture;
}

function OnMouseExit(){
	if (state > 0) state--;
	if (state == 0) myGUITexture.texture = normalTexture;
}

Now the problem is that it wont move. Played around a bit wih the vars and movement on other angles is no problem. The only problem is that the var gets turned into 0 all the time. Anybody got a clue:face_with_spiral_eyes:

If you divide a number by a larger number you will get a value between 0 and 1. If you then convert that to an int, it will be truncated to 0. Try defining xlocA as a float instead of an int.

Thank you, Made some changes, so now I work with iTween, and still got the same problem, going to try to use the float now.

Edit:
Still get the same problem, seems like it that a float also gets truncated.

I’ve found the solution. Aside from using float/doulbe instead of int it seems I needed to add .0 after a static value. This is because te Math system sees value’s without a decimal as int’s and Screen.width/heigth also returns a int. This will make the Math system use a interger math function, wich always results in an int value. By adding .0 to one of the static value’s you force it to use a float math function instead, resulting in a float value.

for example

Vector3(-205.0/Screen.width,(Screen.height-75.0)/Screen.height,0)

instead of

Vector3(-205/Screen.width,(Screen.height-75)/Screen.height,0)