# My first game i need help

## Recommended Posts

So i began on my first and frankly it is really simple i just want a box to jump up and onto the next platform above it. They problem is when i look up a tutorial they don't get me where i want it. I need to know how to make a moving platform from side to side and then when my play lands on it the platform will center in the middle of the screen and stop so i can keep jumping up to the next platform. I can already make my player jump i also know how to make one way floors so that is covered i just cant figure out the platform.

##### Share on other sites

Well think of what you're doing in simple steps.

First : Move platform from side to side

Second : Detect Player Collision

Third : Center platform and player.

Now that we have what you need to get done laid out in clear view we can start on figuring out the best way forward.

For moving the platform side to side we merely need to change the position of the platform. Now you could move it a certain distance per frame but that will cause it to depend on the player's frame rate. A better solution would be to move the the platform a certain distance over a certain amount of time. I think there is two ways of doing this. The first is lerping the platform, the second being measuring the amount of time the last frame took and add the distance that the platform should of moved onto that. For lerping there is plenty of code all over the place for it so just google it. The second you'll have to start measuring for fps, yet again google is your friend. Now you have the platform moving from the center to one side, you need to set a limit and once the limit has been hit, invert the amount being added to the platform so that it moves backwards. Again have a limit set on the other side and repeat the invert once it reaches the limit. The easiest way for a limit is a if statement.

Now we have the platform moving from side to side we need to check if the player collides with the platform. There's two things you need to think about for this. First does the player touch the platform and second is the player falling. If the player jumps and hits the platform underneath you don't want it to magically appear onto and in the middle. So the falling bit is easy if you have the jumping script already done. Just check to see if the movement on the y axis is minus or something along those lines. The collision is a bit harder. I believe unity has its own collision system for 2d and 3d programs so I would look at a collision tutorial for what you require. Once you can detect the collision, check if the player is falling.

To center the player and platform the easiest way would be to have a predetermined middle point and just move the platform there and disable the platform's moving. Whether this is by having a simple boolean or setting the platform's movement to 0 it is up to you ( though the first is more efficient). Move both the player and platform to this middle point (I would just adjust the x axis and leave the player and platform on the same y as they are when they collide). Once they have been moved it is important to remember to set the player's falling to 0 or turn it off and re-enable jumping (depends on how you have your jumping system set up).

Hopefully this is a good enough walk through for you. I don't really want to write the code as it would take a little bit of set up and it is better if you try it yourself because you'll learn more. If you need help google some of the things I talked about or just ask here and post the code you're having troubles with.

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633656
• Total Posts
3013186
×