Hello Guest

Author Topic: How do I position a sprite from a script?  (Read 6783 times)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9644
    • View Profile
How do I position a sprite from a script?
« on: January 26, 2012, 07:09:44 PM »
Use Camera.ScreenToWorldPoint(x, y, distanceFromCamera); x and y are screen space coordinates starting at the lower left. distanceFromCamera is the distance of the sprite from the camera (for occlusion, etc). This works with perspective and ortho cameras.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9644
    • View Profile
Re: How do I position a sprite from a script?
« Reply #1 on: July 12, 2012, 04:18:35 PM »
Important

When using tk2dCamera, it usually isn't necessary to do any of this. By default tk2dCamera is set up to map 1 pixel to one world unit. So, to position something at 150, 30 in screen space (assuming the camera is at 0, 0, 0 and not been moved/rotated, simply set the position to 150, 30, cameraDepth.

The origin of tk2dCamera is at the bottom left, so the above code will position the sprite at 150, 30 pixel units from the bottom left of the screen.

You still need to take into consideration the position of the tk2dCamera, and the rotation if you've rotated it.