tiistai 7. marraskuuta 2017

Unity platformer - moving platforms and parenting

NME17Ap

lukuvuosi 2017-2018

There are multiple ways to do moving platforms with playmaker. The easiest is to use I Tween actions. Start by creating FSM for the moving platform. In this FSM we need two states. Link the states with finished event. 


Now in the first state you need to find action called I Tween Move To. This automatically moves the object to desired position. Select the vector position according to your object transform. If you want to move object vertically on y axis then change those values and same goes for horizontal x axis. I wanted the platform to move horizontally so I changed the values on the x axis. Remember to change the y axis value too, if your object has other than 0 for the value on that axis.  Then define the speed you want for your platform movement.  Remove check marks from orient to path and move to path. Remember to put your finished event in finish event location. Then copy the action to the other state but change the value for the x axis so it matches the original position of the platform.


Second way to do a moving platform is with animation. You need to choose object you want to animate and in the animation window click create new animation. You'll need to create folder in the project for your animations and save the animation under appropriate name.




Next you are going to make the animation. This is done by adding property and choosing transform position. Click on the time line and create a new key by moving the platform to the desired position. Do this few times and make sure the animation goes smoothly back to the position it started from. You can copy keyframes by choosing a frame and pressing ctrl + c. Then choose other place from the timeline and press ctrl + v to paste the frame.



You can test the animation by pressing play button from the animation window. When you are satisfied with the result stop recording by pressing the record button (red dot) in the animation window. Now when you test the game the animations you have made, they will play automatically.


Last thing we are going to do is make the platforms parented with the character. If this is not done and the character is on the platform, it will not move along with the platform. This might work as a feature in the game for horizontally moving platforms. For the vertically moving platforms it might not be a good idea because it can cause some twitching. So that's why you should use parenting for moving platforms.

Create new FSM for the character. In the first state use collision 2D event action.  You'll have to create new tag for the moving platforms because this action uses collide tag to recognize which object you are colliding with. You'll also need to create new variable. This time the variable type needs to be gameobject because you're going to use it for storing the collider. When you store the collider, you can use it later in the next state. Then we need to sent event to activate the next state.



In the next state use set parent action. Use the gameobject variable we created before. This will set the parent according to the last moving platform the character has collided with. Then you want to add one state before returning to the start state. In that state use the action wait for 0.1 seconds. This prevents creating an infinite loop which will result in error.


You'll also need to make changes in the jumping FSM. In the third state you need to create two new actions. One is collision 2D event because you want to prevent the player for using jump while in air with the moving platforms too. Then there's the set parent action. This time don't choose anything for the parent. It will make the character independent object again and it won't move with the platforms while jumping.

Ei kommentteja:

Lähetä kommentti