How To Get Player To Stay On Moving Platform? [2D]

Updated on July 30, 2014 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
14 on July 30, 2014

I’ve been following Brackey’s tutorials up until tutorial 7, then I decided to branch off the project and start my own game.  I am basically starting from scratch save for the movement and camera follow scripts.  I am choosing to make a platformer, and I thought that using iTween to animate my platforms (make them move from left and right) would be a great idea.

Everything was going fine, until I tested it.  When the Player Controller lands on the moving platform, his position doesn’t update with the position with the platform (so if the platform is moving right, the player will eventually fall off).  I’ve been searching the internet for the past two hours, and trying any solutions that didn’t require heavy coding.  One solution that “sortof” worked was making the player a child of the platform object on TriggerEnter2D.  Although, this felt sluggish performance-wise and wasn’t as “smooth” as I’d hoped.  I noticed people posting scripts for other movement scripts as a base, but I want to stick with Brackey’s 2D Tutorial Character Controller.

How would I go about implementing script that keeps the player moving along the platform for Brackey’s script (that wouldn’t conflict with iTween)?  I would prefer to write the script in C# if at all possible.

tl;dr  I need help with moving the player along with a moving platform USING Brackey’s player controller script (while hopefully keeping it simple).

  • Liked by
Reply
5 on July 30, 2014

Hi Brodyzera, watch this video it may help you 🙂
Moving Platforms

I know it is long, but is always good to see something well explained. ^^ 🙂

on July 30, 2014

The video you linked only went into the creation of moving platforms (which I already have).  I went further into the 3DBuzz playlist and found a fix for the anchor issue, but it was specific to the movement controller created in the past few videos.  Thanks for pointing those out though, I’ll watch them later for information!

Guru
on July 30, 2014

I think the best way is to simply add a collider to the player that detects when you collide with a platform that has a tag on it and would make the player be a child of the platform. Then if the collider detects that you exit the collision remove the player as a child. 🙂

on July 30, 2014

Already mentioned that in my OP;

“One solution that “sortof” worked was making the player a child of the platform object on TriggerEnter2D.  Although, this felt sluggish performance-wise and wasn’t as “smooth” as I’d hoped.”

I tried that…but it wasn’t optimal, an example being that the player would be all jittery when at the edge of said platform.  Thanks for the suggestion though. 😛

Guru
on July 30, 2014

I’ve never experienced any jittery when I this method in a platform on my game to be honest, there has to be something wrong with your collider or the platform. First of all is not OnTriggerEnter2D is a OnCollisionEnter2D, second is not bad for performance at all. Try removing the friction of your platform to see if the jittery stops.

on July 30, 2014

I’ll give OnCollisionEnter2D a shot!

EDIT: Using OnCollisionEnter2D is working A LOT better than OnTriggerEnter2D, but I ran into a small issue.  Since the platform uses a box collider that covers the entire sprite, a player is dragged with the platform if they touch the sides or bottom of the platform.  Would it be alright to use 2 colliders on the platform game object, one that covers the enter sprite and one that could be an edge collider on the top of the game object to cover the OnCollisionEnter2D script?  I’m just wondering if having 2 colliders overlapping would cause any issues.

Show more replies
  • Liked by
Reply
Cancel
2 on July 30, 2014

One possible solution is – while a character is in contact with a platform, take the platform’s Velocity and apply that as a force to the character standing on it. I haven’t tested this, but it works in theory.

on July 30, 2014

Wouldn’t this cause the player to move/bounce up?  Unless you mean applying a downward force onto the player.  I’ll give this a shot as well.

Wise
on July 30, 2014

If the platform is moving up, then sure, it would cause the player to move up, but hopefully not bounce.

Show more replies
  • Liked by
Reply
Cancel
4 on July 30, 2014

you could try adding a physics material with high friction to the platforms:
Relevant Link

on July 30, 2014

Tried adding the physics material to the Collider on the platform, didn’t work. :/  Thanks for the suggestion though!

Master
on July 30, 2014

I haven’t seen Brackeys platformer series, so forgive me for my ignorance, but does the character use a rigidbody?

on July 30, 2014

Yeah, the character uses a rigidbody.

Master
on July 30, 2014

Better idea: 
when the platform moves, add its velocity to the player’s as long as they are standing on it.

the platform will use OnCollisionEnter to detect and add things to a GameObject array (this way if other things, such as crates or whatever, land on the platform, they get affected too). Then OnCollisionExit to detect when something leaves so it can remove them from the array.
(if the only moveable object is the player, you can disregard everything about arrays and just have a standard GameObject variable).
Anything in the array get the platform’s velocity added to it
or, if you’re doing it without arrays, just the variable gets it.

thePlayer.rigidbody,velocity += rigidbody.velocity

Show more replies
  • Liked by
Reply
Cancel