Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A question on how to keep character on moving platform

Discussion in '2D' started by DTheChemist, Dec 31, 2021.

  1. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    Hello all,


    I hope i am asking the question the right way. But im having a hard time keeping my character ON my horizontal moving platforms without sliding off while standing still. Ive seen Youtubers "do it" but the code they showed doesnt seem to work at all or 100% so im asking you experienced Unity folks here. Ill fault myself and just say maybe my vision aint great and im not seeing all the code in the video.


    this is the code i had in my platform from watching a tutorial

    private void OnCollisionEnter2D(Collision2D collision)
    {
    collision.transform.SetParent(transform);


    }


    private void OnCollisionExit2D(Collision2D collision)
    {

    collision.transform.SetParent(null);


    }
    (mind you i saw "Player" as his Object name in hiearchy so i confused as to how "collision" is connected to anything. when i was told thats what the player object is supposed to be)


    the method was supposed to have the player become a parent and attach itself to the platform "like a magnet" to stick there without sliding off as the platform is moving left or right.

    Tags of the player is supposed to help it i guessed later but i wanted to ask here how to do it. But obviously from looking at that code above there is no Tag method at all he did. so im wondering if it was bulls*t in the video

    So with that said if anyone here has succeeded at having their character sit still on a moving platform id really appreciate it
     
    Last edited: Dec 31, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    This stuff is fiddly to get right, as there are a few major points of confusion to consider.

    1. where does this script go? Is it supposed to be on the player or on the platform? It makes a difference in this case and often times if a tutorial is going fast, it can be misleading what is given the script. Poor script naming can also contribute to misunderstanding here.

    2. who is collision.transform ? I always have to re-learn this every time I go into writing some collision / trigger code, just because I seem to always have it backwards, and the documentation isn't always 100% unambiguous. See here:

    https://docs.unity3d.com/ScriptReference/Collision2D.html

    I find that the best way to get this nailed down is to use lots of Debug.Log() statements to print out the names of the items involved in order to prove who is who. For instance if you put Debug.Log() statements in and the code is on the platform and trying to parent to the platform, we know that's wrong. This sort of intel can help you quickly sort out what is going on.
     
  3. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    Well the code there was a method to place IN the moving platform script itself. Thats the one method i tried to go with that seemed easier and less mess. Another youtuber had that kind of code in his character movement script and i didnt like that idea, plus when i tried it i didnt get a result.

    The youtuber said collision is just there and made up. can be anything. that didnt sound right to me lol

    Funny you mentioned "Trigger" because one youtuber made his Character stick on platform with the expressions "OnTriggerEnter2D" while the other had his like "OnCollisionEnter2D". I was literally getting annoyed by this because then i was confused on whats the proper method expression to use then?

    so yeah watching Youtubers comes with its headaches at times but a good 40% of them if gotten some clean success using their methods. Enemy AI. Character attacking. losing health. dying etc. The Doc you showed is useful since it shows me a small example with certain expressions with colliders but the actual method to pull off what im trying to do is gonna be a tough one. i might just ignore it from now until i find something i can grasp much easier.

    Thanks for the reply. ill go back to the docs you showed and compare notes. Ive gotten my project to do a bunch of things so far and even a stage clear on trigger method when it collides with an exit so i guess i gotta play with these codes more and keep looking at these docs for other ways to pull off what i want with this specific issue
     
    Last edited: Jan 1, 2022
  4. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    Kurt-Dekker likes this.
  5. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115

    @Valjuin
    :D i literally just got finished watching that same one and was going to report back here to mention i got it to work finally. Thanks anyway! Yeah that youtuber out of the 5 i watched trying to explain this same thing is ONLY one that made it way easier to follow smh. currently im trying to watch his other portion where he shows how to fix the character to not stick poorly and drag on the platform. I got it to finally stand in one place while moving but yeah he walks slow on it. so im about to follow it right now. im Almost there! 99% fix so far.
     
    Last edited: Jan 2, 2022
  6. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    @Valjuin Ok it didnt 100% work for me still for whatever reason. But have you pulled this off in your own way? could you share some knowledge of this? i almost had it. the issue still is the character moves super turtle slow now when sticking to the moving platform. I tried to look over the video multiple times to see what im visually missing but it aint much. Unless its something from another script connected to the ground check for it (im majorly guessing) that he didnt show thats relevant to that video too that i have to adjust a certain way. At times that makes a difference too. But i could be wrong with that.

    So i found a different video to see what happens and its the same slow down result. so by using this new code example ill share that here with the video result test. Btw yes i used two colliders and made one "Is Trigger". So thats where im drawing a blank on this now understanding the method for this. For the record in past examples i saw a youtuber code the tag of both the Player and name tag of the Platform as well. That never gave me results so thats what led me to watching Coding in Flow youtuber's method during the rabbit hole search

    The code (and this is placed in the platform sprite section with its collider). The code that makes the platform sprite move is in its Parent object portion above it that its attached to. and yes the platform sprite with the collider has the layer of "Ground" properly. And yes my Character object tag is "Player".

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.tag == "Player")
    4.         {
    5.             other.transform.parent = transform;
    6.         }
    7.     }
    8.     private void OnTriggerExit2D(Collider2D other)
    9.     {
    10.         if (other.tag == "Player")
    11.         {
    12.             other.transform.parent = null;
    13.         }
    14.  
    15.     }
     
    Last edited: Jan 3, 2022
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    That is just so weird... that's what I mean about "fiddly!" I am sure it will turn out to be some little silly difference between how you are moving the character, or perhaps friction???

    Either way, you will find it. To help you, I just jumped in and blasted out a quick example with a Rigidbody2D left/right/jump controller and it appears to work pretty solidly. Please find it enclosed for your reference.

    I didn't use tags or layers, I just have an
    IdentifyPlayer
    and
    IdentifyGround
    MonoBehaviour that I spackle around the scene appropriately.

    And yes, I'm using the two-collider, one as trigger approach from the above video. I am not putting ANY PhysicMaterial2Ds on anything either.

    Screen Shot 2022-01-02 at 6.49.07 PM.png
     

    Attached Files:

  8. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    @kurt

    Thanks on this. It works. i just have to adjust it to my character ground detection. thats what it was really about. I never seen that method of just adding in an empty script detector for Player or Ground that works. Codes and methods with Unity is crazy. so many ways to do one thing lol smh I have a main project build but i have this in my test build to see how polished i can get everything mirrored and the transfer it over.


    I followed you on twitter. I usually follow a bunch of devs and tinkerers of game development
     
    Kurt-Dekker likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    You're very welcome of course!

    It's super-handy because Tags are global to project, so I would have to tell you "Be sure to set up X and such and this and that tag." This way you can just find the ground or the player quickly via code. Plus you could mix and match them, eg, a player could also be a ground so (for instance) another player could land on him and be carried around by him.

    Agreed. My best advice here is to try your best to read any project as deeply as you can, until you can explain how all the parts (at least the ones you care about) work.

    I am constantly trying new techniques. I purposefully try iterations on my tried-and-true ways of doing things, and obviously do more of the ones that work well and less of the ones that don't. Unity3D is my favorite game engine ever and it's always fun doing new little tests and ideas and jams.
     
  10. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    Definitely will keep digging and studying various projects that are open to look over. I tend to do that on Github at times. i Just download a lil project and pick apart how it works for something im trying to get working. I've been doing that for a while but just not that much luck finding some real across the board solid ones that are similar to what i am trying to pull off. But getting on to methods that you just showed me helps a lot. It can help others also. the more methods to know at the forum the better

    But heres the fix to my dummy test. worked perfectly. ill inject the fix in my real project. i saved it to my go to notes. Thanks!


     
    Last edited: Jan 6, 2022