- Home /
Bouncy/repelling boundaries for a 2D shooter
So I've managed to create a boundary for my player by placing cuboid colliders outside of the bounds of the screen and setting them to have a bounciness of 0.4. This kind of works in that when the players hits it at a relatively high speed it is bounced back, but when the player hits it at a low speed it will scrape along the box weirdly.
I guess what I want is the edges to produce some kind of force that gets stronger the closer to the edge you get until the player can't overcome it, but I really have not the slightest clue how to do this.
I'm scripting in c# if scripting is needed. Any ideas?
Answer by alexi123454 · Jul 21, 2015 at 02:08 PM
You could place a trigger on the barriers that detects when the player is near, and then use
OnTriggerStay(Collider other)
to check how far the player is from the edge of the wall, and do
other.gameObject.GetComponent<Rigidbody>().AddForce(vectorAwayFromWall / distanceFromWall)
The closer they get to the wall, the harder the push to move them away from the wall.
Your answer
Follow this Question
Related Questions
2D Polygon Collider Ball with bounce material not stay completely still 0 Answers
Unity 2018 2D Character Jump Glitch 1 Answer
How to make LineRenderer in GameObject Facing? 1 Answer
HingeJoint2D.jointAngle flips between two values based on rotation 0 Answers
The player randomly freezes in place while other objects move ingame 1 Answer