- Home /
GameObject goes through other objects when clicked and dragged
I have a slight issue here. The player character goes straight through the "walls" rather than stopping. Obviously this is a issue. So is there a way to stop this from happening? The object is dragable by touch/mouse when clicked inside.
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
Answer by allenallenallen · Dec 30, 2015 at 07:02 AM
So basically you want to be able to drag the player but still have it be confined by walls or objects?
Well, your code works fine. It literally drags the player regardless of walls because there's no physics applied. Your approach is wrong.
I guess you can turn on physics and instead of a point based dragging where the player will immediately teleport to where you want, use force dragging. If you drag the character with force, it'll have to stop when it hits walls.
Your answer
Follow this Question
Related Questions
Click and drag jump mechanic 1 Answer
Click and Drag not Clicking or Dragging 2 Answers
Button's Click through problem? Help? 1 Answer
How do i move the slider with GVR reticle pointer? 0 Answers