- Home /
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
Answer by swanne · Nov 30, 2020 at 05:42 PM
Hi. I recommend using OnCollisionEnter2D()
Check out the resource here- https://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html
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.
I am a beginner and what would be the code for the player to move up and down
Hey. Spend some time working through tutorials on Unity Learn or YouTube. There's a lot of resource out there that will teach you everything you want to know.
Your answer
Follow this Question
Related Questions
Moving Player 1 Answer
Move to line 0 Answers
Move a keyframe in time only 1 Answer
Compound Collider Moving Parts? 4 Answers
Jumping with Character Controller?! 1 Answer