Author Topic: *Solved* Transform  (Read 3566 times)

Ingot

  • Guest
*Solved* Transform
« on: June 13, 2012, 04:06:43 PM »
I have a meter that updates when a button is pushed, but I can't get it to update properly.
The method I am using is to grab the transform component of the UISprite object in the
scene and adjust its Y value like this:

  1. public void UpdateMeter() {
  2.                
  3.         _meterTransform.Translate(0,_game.approval,0);
  4. }
  5.  

the _game variable is a reference to the Game script which contains the approval rating.
The meter should adjust its height according to the value stored there. However, when
I update the meter by pressing the button, the Y component of the transform jumps
90,182 units and I don't know why. The puzzling thing is that the approval rating
is 268 and the starting Y position of the sprite is -161. So basically, the meter
should jump to around 268 when the button is pressed. The approval rating is
updating properly (by about 3 - 9) with every button press, but the Y value
goes crazy. Can anyone help me with this please?
« Last Edit: June 13, 2012, 10:32:43 PM by Ingot »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Transform
« Reply #1 on: June 13, 2012, 05:10:00 PM »
It may be setting the global position instead of the local position.

Ingot

  • Guest
Re: Transform
« Reply #2 on: June 13, 2012, 07:25:35 PM »
Ya know, that occurred to me but I don't know how to translate from world to local in this case.
Can you or anyone give me some advice on that?

dlewis

  • Guest
Re: Transform
« Reply #3 on: June 13, 2012, 09:13:06 PM »
Ya know, that occurred to me but I don't know how to translate from world to local in this case.
Can you or anyone give me some advice on that?

You could modify the localPosition directly.

ie.
  1. Vector3 dist = new Vector3(0.0f, _game.approval, 0.0f);
  2.  _meterTransform.localPosition += dist; // May need to go = _meterTransform + dist, not sure if += works (haven't tried myself).

The other option is to tally up all of the non unit scales in the Hierarchy above the game object and multiply the distance by that. Hopefully the only non (1, 1, 1) scale would be the UIRoot.

_game.approval * uiRoot.transform.localScale;
« Last Edit: June 13, 2012, 09:14:58 PM by dlewis »

Ingot

  • Guest
Re: Transform
« Reply #4 on: June 13, 2012, 10:32:27 PM »
That solved my problem, thank you very much.