Hello Guest

Author Topic: ScreenToWorldPoint gives far off results  (Read 8586 times)

Supagoat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
ScreenToWorldPoint gives far off results
« on: July 12, 2012, 05:37:02 am »
I'm brand new (just bought tonight) to 2D Toolkit, and have only been using Unity for a week or so.  I'm still trying things out to see how they work.  I previously had a scene with cube prefabs with textures applied to them that I used a script to instantiate and put in the scene.  (Think procedurally created tile-based map)

I've switched to using 2D Toolkit sprites and have removed the old camera and put in a tk2dCamera.  Now when the script creates the map tiles they're too high by about 20 pixels (and maybe too far to the left too, haven't paid attention to that yet).

I tried using ScreenToWorldPoint as is suggested in the FAQ, but when I do they get positioned at around -900, -700 (and they aren't visible on screen) when I want them at 0,0 screen coordinates. 

Any ideas?

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: ScreenToWorldPoint gives far off results
« Reply #1 on: July 12, 2012, 10:29:16 am »
The coordinate system with the tk2dCamera is 1 world unit = 1 pixel, 0,0 at the bottom left of the screen. (The output is only valid in the game window due to a bug in Unity). So to position it at x:20 y:40, set screen position to 20,40,0. You don't need to use ScreenToWorldPoint here.



Supagoat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: ScreenToWorldPoint gives far off results
« Reply #2 on: July 12, 2012, 03:14:27 pm »
Ohhh, I was thinking it over and I'm 99% sure I know what I did wrong... I bet I had the sprite's origin point set to the middle, so of course if I put it a 64x64 sprite at 0,0 it won't be perfectly aligned with the  bottom of the screen.

BTW - I got the ScreenToWorldPoint info from http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,32.0.html which should probably be updated.  (Newbies like me figure that the tk2d camera counts as an ortho camera)

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: ScreenToWorldPoint gives far off results
« Reply #3 on: July 12, 2012, 03:50:04 pm »
I could've sworn I had updated to reflect the changes with tk2dCamera, but I will now. Thanks for bringing it to my attention.

Kein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: ScreenToWorldPoint gives far off results
« Reply #4 on: August 18, 2017, 02:41:20 am »
The coordinate system with the tk2dCamera is 1 world unit = 1 pixel, 0,0 at the bottom left of the screen. (The output is only valid in the game window due to a bug in Unity). So to position it at x:20 y:40, set screen position to 20,40,0. You don't need to use ScreenToWorldPoint here.

So I'm confused, why in the hell is this even a thing? How to I recalculate Camera.main.ScreenToWorldPoint( Input.mousePosition) now?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: ScreenToWorldPoint gives far off results
« Reply #5 on: August 19, 2017, 03:42:00 am »
Do the same thing. Screen to world point will give you the right position irrespective of how the camera is set up. Tk2d camera supports different set ups now as well - you're replying to a post that is 5 years old...

There was a bug in unity years ago, but that was fixed a log time ago.