- Home /
Getting position of specific collider2D
Hello, I have a huge problem getting one thing to work (I'm not a programmer so I'm struggling a lot with basics often).
First of all here's an image that may help in understanding me. https://www.dropbox.com/s/799ihskfji1sjmd/unityquestion.jpg
Now, I want a game to tell how close is Player to a collider2D (Mesh or Box, I assumed Box will be easier). Earlier I had it solved by adding a trigger sphere collider on a Player and it returned a message when hitting the other collider. Worked perfectly fine but now I want to differantiate messages according to collider relative position to player (above, below, left and right) I tried to do separate colliders for every possibility and give them different tags, what I assume would work but it creates new problem - if the walls are thin then the sphere collider on a player will recognize both above and below or left and right colliders. Therefore I've tried to create if instruction that checks 1. if the collider tag is "above" and 2. if the Y of this collider is less then players Y (more if below and so on). And I don't know how to ask for position of the collider my sphere is colliding with at the very moment. I don't want to add different tags for all the colliders as there can be a lot of them in one scene.
So, if there's an easy solution to my problem I would be really greatful as I cannot sleep without it being solved ;)
Cheers
Answer by Jeff-Kesselman · May 09, 2014 at 05:49 PM
Get the vector between the two colliding objects and check its quadrant.
Vector3 vecToCollision = collision.transfrom.position - transform.position;
if (vectToCollision.X<0) {
if (vecToCollision.Y<0)) {
//top left quad
} else {
//bottom left quad
}
} else {
if (vecToCollision.Y<0)) {
//top right quad
} else {
//bottom right quad
}
}
hey, thank you for your answer unfortunately I'm using trigger, not collision, nevertheless I've solved this problem already by comparing x's and y's of the collider with x's and y's of the the object :) Cheers
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
My Position suddenly changes 0 Answers
Trouble converting transform.position to C# 1 Answer
Adding a position offset to a force towards an object 1 Answer
Camera position not updating 1 Answer