How do I make an object move up automatically and when it touches an invisible object it goes back down, and so on, (from top to bottom)

I want my character to move up automatically and when it touches an invisible object it will come back, and so on

Unity 2d

Hi. I recommend using OnCollisionEnter2D()

Check out the resource here- Unity - Scripting API: Collider2D.OnCollisionEnter2D(Collision2D)

Basically, you’ll have a collider2D on you character, with a tag of “Player” for example, then you will also have a collider2D on your upper and lower invisible objects with tags of “Top” and “Bottom”.

Create a script on your character that moves the object in the direction you want, up for example, and have an OnCollisionEnter2D function which will detect the tag of the upper and lower gameobjects and you can run an IF statement to modify the direction of the character movement depending on which one you collided with.