Categories
Uncategorized

Physics Mayhem

In my forays into Unity physics I am all too tempted to toy with the silly quirks that can manifest. This last week I have been attempting to replicate a mechanic that is common in many games: The clicking and dragging of objects on the ground. It frequently shows itself in puzzle games, and one could argue is the central concept upon which all sim games are built. The ability to place structures or construct buildings is what makes many games fun.

There is an odd problem with implementing these mechanics. While one can clearly see the position of their cursor and the intent of their designs from behind the light of their glowing screen, in the game world the user is moving their cursor on a completely different plane than what their eyes see. A cursor can travel up and down, left and right on the axis of their monitor, and this functions perfectly well in 2D games, however there is a problem when a 3rd dimension is added.

Such cursor movements do not accurately represent depth. A cursor is NOT actually moving along the ground, or distant objects, it’s permanently pasted the the face of your screen. This means it can be difficult to distinguish where objects are actually meant to be deposited by that cursor in 3D space. You may mean to click on the ground a distance away, but the game doesn’t know if you meant to do that, or to place an object right in your face.

This is a problem that inevitably rears it’s head in any game that involves accurate cursor clicks in a 3D space, and fortunately there are multiple solutions to it.

One simple answer is to identify the exact object that your mouse clicked. This is actually relatively simple. Unity can fairly easily identify objects that the mouse is hovering over. This solution works perfectly well in games that require identification of distant objects. Unfortunately it does not provide accurate placement, and so in our case of placing workbenches and tables on a factory floor in deliberate and organized manner, will not suffice.

This led me to find a better solution: Ray tracing! This is a term that I had heard in passing for years, though usually to do with lighting effects in games. The way it works is by drawing a line from the center point of the camera, to the position on the mouse on the screen, and then following the direction it extends. Unity can further identify the next object that the ray will hit. This method allows for fast and easy interaction with a 3D environment.

This will be the method I rely on for the placing of objects in this project, though It can be used for other tasks with further applications. I believe I can use it to move, delete, and even track the position of objects, and I am excited to see what other tricks I can pull in this engine.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *