- Home /
Moving two objects and avoiding overlap with each other
Hi Guys Im Moving two objects with this function:
void OnMouseDrag(){
float mouseX = Input.mousePosition.x;
float mouseY = Input.mousePosition.y;
Vector3 newPosition = Camera.main.ScreenToWorldPoint(new Vector3(mouseX, mouseY, 99));
this.transform.position = newPosition;
}
How can i avoid the objects to overlap with each other? Thanks.
do you mean you want things to collide whilst dragging? try here. I haven't tested this script but its by top man @Eric5h5 so I'm going to assume it works perfectly ^^.
If you mean you want them to overlap and then when you stop dragging they should separate, then on mouse up you should check for overlaps and move things away from each other, there are lots of algorithms around for that sort of thing.
Hi $$anonymous$$an, thanks, but the script by Eric5h5 isnt what i want, yeah it cant drag objects, but isnt collide between objects, i want to collide, because they cant be overlap with other objects. I will try the $$anonymous$$ouseUp Thing, ill tell you it goes.
Answer by MrCakePie · Jan 09, 2015 at 07:23 PM
If you don't want objects to collide with each other then you can put them in different layers and define to ignore the collisions in Project Settings > Physics2D
This can also be done via script.
Sorry but i want then to collide, i dont want them to overlap with each other.
I don't think I understand your problem then, can you rephrase it?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Smooth Move script for my spaceship 0 Answers
Camera move when reach edge of screen 3 Answers
Distribute terrain in zones 3 Answers
C# move main camra via mouse position on boarders around screen script for RTS Game 0 Answers