- Home /
Question is off-topic or not relevant
How do I get the direction of an object relative to the position and rotation of another object?
Okay, So I'm making a bunch of squares in 2D space that you can drag around and I want them to connect to each other when they are dragged into each other. So far all this system has been done but I'm running into a problem, the script cannot find which direction its connecting to upon dragging it to the object, I originally waited until collision and then Would take the two objects position to find out which side its collided with But this seems only to work while the object is aligned with the world. Is there any way to find which direction one object is relative to another?
Example, (Object 2 needs to know its just collided with the left side of object 1)
Answer by sisse008 · Jan 11, 2018 at 12:23 PM
each box should have 4 triggered colliders (up, down, right, left) uppon collision:
if box 2 collider with "down" collider of box 1: box2.position = box1.position - (0, box1.length/2);
if box 2 collider with "up" collider of box 1: box2.position = box1.position + (0, box1.length/2);
if box 2 collider with "left" collider of box 1: box2.position = box1.position - (box1.length/2, 0);
if box 2 collider with "right" collider of box 1: box2.position = box1.position + (box1.length/2, 0);
then, make box2 child of box 1 and make box 2 kinematik
Seemed to work well at first although the same problem appears, if box one is rotated in any way then when trying to position the cube it always ends up in the position based on the world, say I connect it to the bottom trigger and the box is rotated -90 degrees it just simply places the box on the left side. Although I think I can take the position of the 4 trigger colliders and just hand them to the second box on collision.
Follow this Question
Related Questions
Incorrect usage of Vector3.Dot? 0 Answers
move toward but keep going 1 Answer
(2D) MovePosition slows falling 1 Answer
Get information on ignored collision 1 Answer