- Home /
How to make a movement at a distance equal to its diameter, which cannot pass through objects?
Good day, My name is Lisa! I am making a game in which the piece moves a distance equal to its diameter. Watch the video. I try to do the same control, when the "next move preview" cannot go inside another chip, then the "next move preview" decides to change the location to the nearest one where there is free space.
The main question is how to make a movement at a distance equal to its diameter, which cannot pass through objects as in the video? can u help me please
Answer by ilagef · Jul 16, 2020 at 10:23 PM
Try this:
Add RigidBody2D to the objects, set to kinematic. What the player click on the objects, it should disable the kinematic mode. This would make sure that the objects won't overlap when the player drag them:
public void onObjectClick(GameObject selectedObject)
{
selectedObject.GetComponent<Rigidbody2D>().isKinematic = false;
}
public void onObjectClickDown(GameObject selectedObject)
{
selectedObject.GetComponent<Rigidbody2D>().isKinematic = true;
}
To limit the moment by diameter, try to use Vector.MoveTowards:
Vector2.MoveTowards(theDragedObjectPosition, mouseClickPosition, maximumDistance);