Search Unity

Realtime screenposition of a gameobject

Discussion in 'Scripting' started by herbie, Dec 2, 2014.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Hi Everybody,

    I have a large sphere in my Android game which can rotate by touch.
    A second smaller sphere is a child of the large sphere and is places at some distance from the large sphere. So when the large sphere is rotating, the smaller sphere is also rotating.
    The position of the large sphere is (0,0,0) and still remains (0,0,0) when it's rotating (of course). But the centerpoint of the smaller sphere is changing when the large sphere is rotating.

    What I want to have is the realtime screenposition (XY) of the smaller sphere when the system is rotating. Is that possible?
    I have tried a lot (WorldToScreenPoint, transform.position, ....) but I can't see the wood for the trees.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks for reply.

    Camera.WorldToScreenPoint gives the position of the smaller sphere only when the game is started.
    But when I rotate the spheres (so the position of the sphere changes), Camera.WorldToScreenPoint does not give the new position. So the position is not updated (I hope you understand what I mean).
     
  4. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    your smaller sphere is a child of the main one, so you need to calculate its new world position after the rotating,

    try this: http://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html

    i think if you call it with a local/offset position from your main sphere, you'll get the world location of that point, which you can then feed into WorldToScreenPoint
     
  5. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks!
    It's working now!