- Home /
Please Help - 2D Collisions Without Unity's Physics
Hey guys, I'm really stuck, I've been trying to solve this myself for a month now with no luck, I really need your help.
I'm trying to have a player fly through obstacles that are moving towards him (he's stationary), and I have pathways in the obstacles that he's allowed to pass through.
What I need is for him to be able to collide with the top and bottom of those pathways and stop from going further 'inside' the obstacles walls.
Also, I'll need him to die if he doesn't make it into the pathways and instead hits the wall first, but I can probably solve that part myself once I have the path collisions worked out.
Here is what I've tried so far, note the comments, it's the one down the very bottom that needs fixing:
void OnTriggerEnter2D(Collider2D obstacles) {
if (obstacles.gameObject.tag == "Obstacles") {
playerInObstacle = true;
}
if (obstacles.gameObject.tag == "Path") {
playerInPath = true;
path = obstacles;
}
}
void OnTriggerExit2D(Collider2D obstacles) {
if (obstacles.gameObject.tag == "Obstacles") {
playerInObstacle = false;
}
if (obstacles.gameObject.tag == "Path") {
playerInPath = false;
}
}
void Update() {
//a bunch of other movement stuff is actually in this section, if you need that, let me know and I'll post it.
if (ySpeed > ySpeedLimit) {
ySpeed = ySpeedLimit;
} else if (ySpeed < -ySpeedLimit) {
ySpeed = -ySpeedLimit;
}
transform.position += Vector3.up * ySpeed * Time.deltaTime;
//floor collisions (this one works)
if (playerY < Y_Min) {
floorOn = true;
ySpeed = noSpeed;
} else {
floorOn = false;
}
//path & obstacles collisions (this one doesn't work)
if (playerInObstacle == true) {
if (playerInPath == true) {
if (playerY > path.bounds.max.y) {
ySpeed = noSpeed;
} else if (playerY < path.bounds.min.y) {
ySpeed = noSpeed;
}
}
}
}
Thank you very much in advanced, I really appreciate the help!
use raycast ..its good. example
vetor2 direction = rightedge.position -centerpoint.position;// direction = direction.normalized;//gets the direction of ray. // Debug.DrawLine (rightedge.position, centerpoint.position, Color.green); RaycastHit2D RH = Physics2D.Raycast (centerpoint.position, direction, 15f); if(RH.collider != null) { RH.collider.gameObject.setActive(false); //whatever blah blah } if u need more idea,pls let me know....
There was a post on the forums or UA about this very same subject a few months ago, but I can't find it.
You can use Unity physics to achieve that. Just use non-trigger colliders and a rigidbody on the player (just watch out for the functions you use to move it). If the player hits the obstacle from top or bottom, it'll stop moving. If it hits the obstacle from a direction other than top or bottom, it will be pushed backwards (towards a killing collider). No need for OntriggerEnter functions. Just create the obstacles as separate objects ins$$anonymous$$d of objects with holes.
[Edit] Using colliders and OnTriggerEnter/Exit functions already is using Unity's physics.
[Edit 2] I couldn't find the post, but this live session will do. It $$anonymous$$ches the physics of the obstacles.
The first edit you put - But I've used colliders for the floor and used it as triggers and then changed the value of ySpeed and it worked... Wouldn't the same thing work in this situation?
Second edit - Alright I'll check that out, still thinking it should be possible with the direction I was heading though..
@ Bugs Bunny - Thanks, I'm not too sure if what you posted will work or not, I'll have to see if properly formatted, so I'll go try it out. But is that for the up and down on the y direction only?
Answer by ArgoEclipse · Jul 31, 2015 at 06:34 AM
Why not just add a rigid body to the game objects?
I've tried that, but when the player touches the collider, it does a kind of jerking motion. Not a smooth sail across it like I need.
Your answer
Follow this Question
Related Questions
Changing player's moving direction 0 Answers
How can I make a collision sensitive teleporter for a 2D game #c 1 Answer
Need your help by movement 0 Answers
2D Character Switching HELP! 0 Answers
How do you make a player be still? 1 Answer