Hello Guest

Author Topic: How to implement rotating puzzle pieces (a noob's odyssey)  (Read 13225 times)

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
How to implement rotating puzzle pieces (a noob's odyssey)
« on: January 19, 2013, 02:13:33 am »
To learn Unity + 2DTookit I want to make a simple memory game.  The board would be comprised of a 9 x 9 grid.  I have rendered the pieces in POV Ray along with animated rotations. Each piece has a symbol and a backside.

To save space I have only included 1 copy of the backside rotating animation and all of the symbol animations.  I need to animate the back facing card until it is perpendicular to the view plan and then switch to another clip of the symol starting from perpendicular to front facing.  If it is not a match I need to go from front facing to perpendicular and then back facing again but completing a full 360 degrees of the rotation.

It's a total of 4 clips and 30 frames.

1) Can you string clips together or is it better to create long clips of everything I want to do?
2) Do I create a master grid array and load animations into each grid point and then swap out the clips in code

I know these are basic questions and I don't want people to write the code for me I just want to understand maybe what some of the approaches would be and what the tradeoffs are.

Thanks in advance for any help.

-E
« Last Edit: January 19, 2013, 02:53:13 am by Mezcaholic »

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (A noob's odyssey)
« Reply #1 on: January 19, 2013, 02:52:42 am »
It looks like picking through Demo 5 will get me a long way towards asking better questions.  I'll update with my findings.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #2 on: January 19, 2013, 04:24:00 pm »
Yup, was gonna suggest the same thing. The demo goes through most of the features.

To answer your questions:
1. You can do either. You can string clips together using animation complete delegates to start playing the next clip after the first one is down.
2. That should work fine.

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #3 on: January 19, 2013, 07:57:27 pm »
Thanks again for your reply.  Yes demo 5 was very helpful.  The message sample threw me for a loop until I saw that in the clip editor in the timeline there was one small line above the frames and that is where the triggered even was.  That's a pretty sweet setup.

So I get creating a game object in the editor and then adding the components in the GUI and manipulating it from code(sort of) :)  But, if I want my game to support 3 x 3 to say 10 x 10 grids how do I create these programatically?  I'm trying to figure out how I would construct this on the fly create the GameObjects that I need when I need them.

Noob on a mission,
-E

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #4 on: January 19, 2013, 08:05:40 pm »
Create a prefab, store a link to the prefab to your game controller object, and Instantiate as may instances as you need. Check the unity docs on how to do this. Once you grasp this concept a you'll find it is very powerful indeed.

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #5 on: January 21, 2013, 07:54:59 pm »
Thanks again Unikron, I worked through that and I have my board populating from prefabs.  You are right, this is very powerful.

I'm now at an implementation issue.  The images on the board need to be buttons or something like them.  I need to get notified if someone clicks on it so that I can begin the animation sequence.  I'm not quite sure how to do this.  Do I pass GUILayout.Button() an invisible image the size of my images?

I also messed around with GUILayout for well layout and it works will but I don't know to know where the buttons are laid out.  So when I do get my button press I won't be able to draw my animation where the button was.  There is probably a straight forward way to approach this but I have not found it yet.

-E

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #6 on: January 22, 2013, 06:32:27 am »
Looks like I am having another issue.  When I instantiated my prefabs before I was not collecting the returned object.  I'm using C# btw.  So I created a tk2dAnimatedSprite called mySprite and then I did something like

mySprite = (tk2dAnimatedSprite) Instanciate(gridCell, Vector3.zero, Quaternion.identity);

This give me a bad cast error, so then I changed it from tkwdAnimatedSprite to (Transform) and I get past the cast but when I try to use the animation it says there is no animation attached to the Clone.  So if I instantiate a bunch of prefabs clones how do I go about addressing them and manipulating them?

This is probably something basic but I kind of stuck at this point.

Thanks for any help.

-E

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #7 on: January 22, 2013, 07:02:32 am »
Ok progress.  I guess setting up my public prefab variable as a Transform was a no no.  I have set it up as a tk2dAnimatedSprite and at least I am getting results that I would expect.  The current plan is to create a list of my animation objects and then in my OnGUI routine create invisible buttons over each of these.

Then key the animations off of clicks on these.  If there are better ways to do this I would appreciate a heads up from anyone really.  Also, if this is not the proper forum for these kinds of questions let me know too.  I don't want to gum up the works.

Thanks,
-E

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #8 on: January 22, 2013, 12:07:26 pm »
Each gameobject in Unity consists of > 1 component. It MUST at least have a transform component.

When you create a reference to an object by using a child component, instantiate will return that particular object. If your reference is a GameObject, it will return a GameObject. If you reference a Transform, you get a transform back, and so on.

If you store a reference to a GameObject, you can use something like this to get to an attached component you don't have a reference to.
GameObject go = Instantiate(...) as GameObject;
tk2dAnimatedSprite animSprite = go.GetComponent<tk2dAnimatedSprite>();


To anwer your second question about clicking and stuff, keep away from the GUILayout stuff. Look at the tk2dButton sample. You could probably use that as a basis to implement what you need exactly.

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #9 on: January 23, 2013, 07:30:33 am »
Yup, I have that part working now.  I just have to skin my knees a bunch but I'm picking up speed.  Things are looking pretty good.  I've changed to project to a Sudoku game, similar layout.  But now I want to group my tiles in mini grids.  I have the code working, it's essentially code I had running on XNA.

However, in XNA the 2DTexture used screen coordinates to draw with.  I found a utility function on the Unity forums that said it did the screen coordinate conversion.  I had to modify it for it to somewhat work for me.  You replied to that post saying that you had added a utility function as well that handled different camera types and such. What's the name of it?

Also, my scaling is not working properly.  In XNA I had everything lining up regardless of screen res, but currently I have gaps between the tiles of my mini grids.  I guess I need to figure out how to derive the proper scaling in the local bounds.

Anyway thanks a bunch, things really are progressing and I find your toolkit to be everything that has been advertised.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #10 on: January 23, 2013, 09:12:36 am »
Do you use the tk2dCamera? If so, then the world positions = screen coordinates. Should be a whole lot easier to deal with in a game like yours.

What kind of gaps? If its really tiny black gaps, have you done this? http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,294.0.html

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #11 on: January 25, 2013, 05:03:55 am »
Ok, I switched to tk2dCamera and my coordinates are good now.  The gaps are gone since I followed the instructions at the link you passed me.  I am looking through your button code to try to get something going with that.

Main problem I have right now is that my animations are too small.  At 1920 x 1080 I have to scale them up 400 x and y to get them to the anticipated size.  When the resolution goes up or down from there there are gaps again.  Not the one pixel something funny is going on gaps more like I need to scale those things again.  I was expecting the size of my sprites in pixels to translate to actual pixels.  I'm definitely doing something wrong.

-E

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #12 on: January 25, 2013, 09:54:13 am »
Yup. Go into your sprite collection, and in settings tick Use tk2dCamera and commit. Everything should be the right size now.

Mezcaholic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #13 on: January 25, 2013, 03:59:12 pm »
That's it, I am at 100% feature parity with what I had in XNA plus I have a dynamic background.  Your toolkit is awesome and your support is even better.  I have the buttons implemented and invisible overlaid my board cells.  I'll figure out how to attach them to actions I need.

Now to another bit of functionality.  I need to create 2 custom controls. The first is a carousel control for the symbols that can be placed in the cells.  I have a vision for what I want but I don't know the best way to implement it.  The control should be 3D or at least appear 3D.

Is it best to actually place the carousel pieces with Z depth for the 3d Control?  Would I need a separate camera for that since my main camera is ortho? If so how do I combine the outputs of the 2 cameras? 

If an actual 3D control is not practical I can simulate it with scaling and vertical offsetting and I think I can figure out the rest.

As always thanks for your help unikron,
-E

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to implement rotating puzzle pieces (a noob's odyssey)
« Reply #14 on: January 25, 2013, 08:16:22 pm »
Its up to you - with what you're planning you probably need a perspective camera. Doing this is easy enough, but its not easy to get close to 1:1 mapping with a perspective camera + viewport. A much easier solution will be to do it in 2D like you mentioned.