- Home /
Moving Platform issue
Hello, My issue is that when I get on the moving platform my character's animation will start to freak out and flicker like crazy. I found out that it was shrinking every second frame when hitting the OnCollisionEnter2D and stops when the player jumps off the platform. The Player in the Hierarchy also jumps around like crazy from being a child of the platform too not being one.`
void OnCollisionEnter2D(Collision2D other){
if(other.gameObject.tag == "Player"){
Debug.Log("On The Platform");
other.collider.transform.SetParent(transform);
}
}
void OnCollisionExit2D(Collision2D other){
if(other.gameObject.tag == "Player"){
Debug.Log("Off The Platform");
other.collider.transform.SetParent(null);
}
}`
This is the first time this has happen to me. I thank anyone that comes along to help. Thank You.
Answer by Stratosome · Jul 23, 2018 at 03:40 PM
Hiya! :D
Alrighty, so if I were to guess what the issue is here, it's because as the platform is moving and the player is on the platform, there are brief instances where the player actually STOPS COLLIDING with the platform triggering the OnCollisionExit2D function, but because the player is still over the platform, they will fall back on to it and trigger the OnCollisionEnter2D. Using OnCollisionEnter/Exit fires off once when something either collides or stops colliding, and for this type of thing you are doing, it is going to be imprecise. I would recommend using OnTriggerEnter2D and OnTriggerExit2D instead of the collision ones. With these, you can define an area around the platform (probably just right above it where the player stands) and as long as they are standing inside of that area, it isn't going to jump back to OnTriggerExit2D and freak out like you described. It is a lot easier to handle the OnTrigger functions than the OnCollision ones. Look in to those and see if that helps. If not, let me know and I'll come up with something else useful to say hah.
Thank you for the reply. I did what you said and changed the OnCollisionEnter2D/Exit with OnTriggerEnter2D/Exit but it still didn't fix it. So made a new sprite at it's original size and found that if you shrink something then make a object a child of that object when the game is playing it will shrink that child object to it's size i.e making my sprite freak out the way it's doing. So I just have to make the platform sprite small before importing it into Unity. Big Thanks.
Oh okay, glad you have something figured out! Just changing the OnCollisions to OnTriggers wasn't all you had to do though. You would need to add colliders to the platforms and then make them triggers (in the inspector window) for this way to work.