Unity Distance Controller

by davidjans in Circuits > Arduino

230 Views, 0 Favorites, 0 Comments

Unity Distance Controller

unity distance controller

This is a type of controller that moves a type of object in unity (In the example a cube) I created this for a school project and wanted to share it :)

Inside the instructable you will find all the code in the project besides the side scroller character controller.

Supplies

4 Ultrasonic distance detectors

1 arduino uno r3

8 cables

materials for the casing.

Find a Casing Type You Like.

20220517_215929.jpg
20220613_214902.jpg
20220613_214853.jpg

I chose a cube with cutouts in the bottom but just glueing it onto a random small surface works regardless.

Preferably you have a area to place it in that is as smooth as possible so it is more accurate and less prone to issues. But a cardboard box works good enough to test it out. I recommend having a slightly elevated area around the edge so the ultrasonic doesn't go all the way to the edge (That causes some inaccuracies with the distance detection).

Set Up Your Arduino

20220614_1221472.jpg

Place the arduino in the middle.

Place the sesnors to the right and front of the arduino ( The back is where the usb cable goes)

Upload the attached code to your arduino

Setting Up Unity

Download any unity version above the 2021 lts version.

Create a new unity project or open an existing one.

Download the attached unitypackage.

Once a project is opened double click on the unitypackage and it will import all the code i used for a small demo besides the character controller.

Setting Up Within Unity

image_2022-06-14_154541277.png

When the unitypackage has been imported you can drag the prefab into any scene.

This prefab should include:

A movement cube

Finish, death,checkpoint and respawn point.

A camera.

The settings are ment to:

Offset: Give an offset from a 0.0.0 point

X/Y clamp: Is the maximum and minimum point it can reach in the level for their respective directions,

Max/Min X/Y Distance: the maximum and minimum distance the controller can go in real life. This is something you have t odebug yourself by looking at the display under the ultrasonic.

Average number: the ammount of positions it needs to keep track of. It takes the average of these positions to smooth out and imperfections in the detection distances provided. The higher the number the smoother (and slower) the cube will move in the game.