Hello Guest

Author Topic: tk2d camera questions  (Read 12438 times)

dotty

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 65
    • View Profile
tk2d camera questions
« on: July 15, 2012, 10:33:58 pm »
I'm confused by the tk2d camera. I'm reading the documentation over at http://www.unikronsoftware.com/2dtoolkit/doc/tutorial/tk2dCamera.html and it says the default camera size is 480x320.

I made 2 sprites from a 480x320 canvas and exported them as PNGs. These can be found at http://imgur.com/a/OfoHU

So, I added the camera (leaving the defaults as they are), and created 2 sprites from the 2 images (I set the atlas to use the tk2d camera).

I added the 2 sprites at the position 0,0 (lower left). However, the sprites appear off camera? Also they appear smaller? Why is this? I've done to know what I have done wrong.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d camera questions
« Reply #1 on: July 15, 2012, 11:26:31 pm »
The main confusing with the tk2dCamera is the fact that the Unity Camera preview is bugged, and won't show the output correctly when a custom projection matrix is used. I've reported this to Unity but haven't had any note of a resolution yet.

Always look at the Game view for an accurate representation of what is happening.

A few tips to make sure what you're seeing is what you'll get when running the game.
1. Make sure the game window is set to the correct resolution, or at least the correct aspect ratio.
2. Make sure "Force Editor Resolution" is set, and WIdth and Height at the target resolution you are previewing.
3. Delete overrides when you don't need them.

Your sprite should appear 1:1 in this window (assuming you set the correct resolution in the game window). If your window is bigger or smaller, the sprite will be scaled to preview what would be seen at that resolution.

Hope that helps.

dotty

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 65
    • View Profile
Re: tk2d camera questions
« Reply #2 on: July 15, 2012, 11:38:18 pm »
What I had to do was set the sprite scales to 2. This seemed to fix all my issues (apart from the editor bug). The game window now works fine.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d camera questions
« Reply #3 on: July 16, 2012, 12:12:57 am »
You shouldn't have to (and shouldn't) do that. Doing that means something else will break when you change resolution. Doesn't really matter if you don't care about other resolutions.

dotty

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 65
    • View Profile
Re: tk2d camera questions
« Reply #4 on: July 17, 2012, 10:36:42 pm »
Thanks for the reply. I'm happy leaving it as is (more of a test at the moment).

I got everything working fine, however I have a strange issue. I created some guiText and positioned it in my view (I can see it fine when I'm in game view, or the app is pushed to my android phone).

However, I have a simple script to take the touch X position and replace the guitext's value with the X position. If i touch the far left I get 0 (or close enough to 0). However, when I touch the far right, the value is 795 (or close enough).

How is this possible? I thought the resolution was only 480?

Here's my code.

Code: [Select]
if( Input.GetTouch(0).phase == TouchPhase.Began ){
var touch = Input.GetTouch(0);
TextMesh text = GameObject.FindGameObjectWithTag("debug").GetComponent("TextMesh") as TextMesh;
text.text = touch.position.x.ToString();
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d camera questions
« Reply #5 on: July 18, 2012, 12:33:13 am »
Hey this is a "feature" in Unity. Let me try to explain whats going on.

Unity returns the actual position in mouse/touch units, origin at the bottom left of the screen much like the tk2dCamera.

There is a serious issue with using the values directly, as this is dependent on the number of pixels on screen and doesn't map to the viewport size parameter at all. For instance if you set the game viewport resolution to 960x640, and then make the game window really small - the values returned have no relation to that number. The number returned is in screen pixels, so if you resized your game window to 240x160, then the Input values will be in that unit.

Hope that makes sense.

You'll only get correct numbers when the viewport is the right size, or scale by Screen.width & height, and then use that normalized data to scale to the correct resolution. Or use raycasts, thats a whole lot easier.

dotty

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 65
    • View Profile
Re: tk2d camera questions
« Reply #6 on: July 18, 2012, 10:10:04 am »
Is there an example of using ray casts to get the position?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d camera questions
« Reply #7 on: July 18, 2012, 09:12:44 pm »
Check the Unity docs for an example. http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
You could try firing a ray from the player to the ground and try to get him to not fall through the ground as a test...