- Home /
GUITextures not lining up correctly
I'm making an Action RPG. Weird thing happening when my model's Transform is changing (meaning: while it's moving around). When my player gets within a certain radius of this model, a "panel" of sorts slides in from the left side of the screen showing its stats (it's name and health). While the model is stationary, everything looks fine, but when the model is moving, sometimes the different GUITextures that make up the panel don't line up correctly. Why? Because the Game Object that displays these GUITextures is rotated for some reason! Below is my functon:
void OnTriggerEnter(Collider e)
{
if (e.gameObject.CompareTag("Player") && EnemyYPosition.getEnemyCount() < 4)
{
attacker = e.gameObject.GetComponent<Transform>();
ep = (Transform)Instantiate(ePanel, Vector3.zero, transform.rotation);
ep.Find("Name").guiText.text = gameObject.name;
players.Add(e.gameObject);
}
}
"ep" is the panel Prefab which has a GUITexture component. It also has two children: an object with just a GUIText (that displays the model's name), and another object with a GUITexture (which displays a gradient bar of variable length called the health bar). Since they're all group as one object, they should all move together, but when my model's movement script is activated, sometimes the different child object end up in different positions. Here are the relevant functions in the script attached to the panel object:
IEnumerator Start() { gameObject.transform.position = lossPos; time1 = Time.time; time2 = 0.0f; StartCoroutine(Translation(transform, newPos, 1.0f, MoveType.Time)); while (true) { g.pixelInset = new Rect(g.pixelInset.x, g.pixelInset.y, xHP * guiHP, g.pixelInset.height); //StartCoroutine(EnemyPanelPosition()); yield return null; } }
IEnumerator Translation (Transform thisTransform, Vector3 endPos, float value, MoveType moveType) { StartCoroutine(Translation(thisTransform, thisTransform.position, endPos, value, moveType)); yield return null; }
IEnumerator Translation (Transform thisTransform, Vector3 startPos, Vector3 endPos, float value, MoveType moveType) { float rate = (moveType == MoveType.Time)? 1.0f/value : 1.0f/Vector3.Distance(startPos, endPos) value; float t = 0.0f; while (t < 1.0f) { t += Time.deltaTime rate; thisTransform.position = Vector3.Lerp(startPos, endPos, t); yield return null; } }
If anyone can help, that would be great.
Answer by cyangamer 1 · Jul 15, 2010 at 06:31 AM
I've now fixed it by putting this command in the Start() method:
gameObject.transform.eulerAngles = Vector3.zero;
Now, the object will always face the same direction when instantiated!
In that case, you could try something else ins$$anonymous$$d - change the last parameter of the instantiation to Quaternion.identity (And try to always use Quaternion.identity ins$$anonymous$$d of eulerAngles = Vector3.zero )
Answer by Mike 3 · Jul 14, 2010 at 09:30 AM
Add this after the while loop:
thisTransform.position = endPos;
You were only setting the position to the value of t before it hit 1
Thanks. This may have inadvertently fixed something else, but it didn't fix my problem. After running some more tests, I have figured out what the problem is. I will alter my question shortly to reflect what the actual problem is, because I still don't know how to fix it.