- Home /
How would you make a pulling/sucking/attracting mechanic?
So right now i'm trying to recreate kirby essentially and i've got his general movement down but i'm at a loss as to what to do with the inhale mechanic. My initial idea was to use an empty game object that is attached to my player and making it so that if an enemy is inside of that and a button is pressed, a bool returns true and the enemies transform position starts going towards the player. Thing is, I don't really know how to make this work.
My first thought was to use tags but I think that would be a little limiting and I feel like there is probably a way to do it by just having the empty get what ever object is in collision with it be affected while checking to see if it isn't something that can't be sucked (such as gordos or bosses).
Could you show me the way you did it? I'm also doing kirby
Answer by petur · Nov 04, 2019 at 12:30 AM
I would use OverlapCircle (https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html) or any other "overlap" function to get all the colliders in the area in front of the player. Then iterate through them identifying which ones can be absorbed.
This can be decided using a tag like you said, but also checking for the pressence of a certain script in each object, (for example, create a "Swallable" script and add it to swallable enemies) with ".GetComponent()" you can get said script, if it is not null then the gameobject can be absorbed.
You can add said objects to a list of objects currently being absorbed and keep moving them closer to the player in the Update() function. You can also use the physics engine and apply a force towards the player to them.
Thanks for the suggestion, I didn't think you could use overlap for that.
A nitpick, but the word you are looking for is swallowable
Answer by MegasonicZX · Nov 23, 2019 at 12:45 AM
So, I know it's a bit late to be asking this (i've been busy so it's been a while getting back to this) but when I try to do this, I get an error that says "Foreach variable cannot operate on variables of type Collider2D because collider2D does not contain a public instance definition for GetEnumerator". What does this mean exactly? I tried to do it with just a regular collider too but I don't think I was picking up anything with that. For reference i'm trying to use the foreach method with this.
Lol, I literally just sat down, tried a simpler method and figured it out in just a few seconds. I guess it's true what they say, you really do just need to step away for a while to get the answer to coding problems. Funny thing is, I think the only reason it stumped me was because I started looking through other sources to figure out precisely what to do when all I literally had to do was do exactly as you said. Thanks again petur.
Your answer
Follow this Question
Related Questions
How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer
2D moving platforms, physics issues 1 Answer
[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 0 Answers
Jumping from special jump orb 1 Answer
How do I make a physics based elevator? 2 Answers