...
The goal of this tutorial is to teach you how to create a Revolution Counter by rigging a DC motor to rotate a flywheel with a hole. A Beam Receiver triggers a counter script when it detects a beam, from the Beam Emitter, passing through the hole.
You will need to:
Create a Kinematics Controller.
Add a WheelJoint to the Axis object.
Add a DCMotor to the Motor object.
Add a BeamEmitter to the Emitter Lens object.
Add a BeamReceiver to the Receiver Eye object.
Have the BeamReceiver execute an action when the laser is detected.
...
Project overview
The project consists of a predesigned model that has been optimized and correctly put into the Hierarchy. Textual reference has been added to each of the steps, please find these under the Revolution Counter Tutorial. The textual reference is displayed in the Inspector.
...
If you want to use Kinematics, you will firstly need a Kinematics Controller https://unit040.atlassian.net/wiki/spaces/PUD/pages/280954380 . This component controls all Kinematic Transforms in the scene.
...
Step 2. Add a Wheel Joint to the Axis object
To create a Wheel Joint https://unit040.atlassian.net/wiki/spaces/PUD/pages/281673729 and assign it to a GameObject, select the GameObject and go to Mechanics > Add Kinematics > Wheel Joint.
...
Step 4. Add a Emitter to the Emitter Lens Object
To create a Emitter https://unit040.atlassian.net/wiki/spaces/PUD/pages/280986794 and assign it to a GameObject, select the GameObject and go to Standard Components > Beam Sensor > Emitter > Add Physical Component.
...
Step 5. Add a Receiver to the Receiver Eye Object
To create a Receiver https://unit040.atlassian.net/wiki/spaces/PUD/pages/280954035 and assign it to a GameObject, select the GameObject and go to Standard Components > Beam Sensor > Receiver > Add Physical Component.
...
Step 6. Have the Beam Receiver execute an action when the laser is detected.
Code Block | ||
---|---|---|
| ||
using UnityEngine; /// <summary> /// Simple revolution counter script /// </summary> public class RevolutionCounter : MonoBehaviour { /// <summary> /// Number of revolutions since start of Play Mode /// </summary> private int revolutions = 0; /// <summary> /// Counts revolutions and outputs the amount in the console /// </summary> public void CountRevolution() { revolutions++; Debug.Log("Revolutions counted: " + revolutions); } } |
Save the code above as RevolutionCounter.cs somewhere in your project. Add this script as a component to the Revolution Counter GameObject.
Now we will add the action to the Beam Receiver when it detects a signal. Go to the Properties tab of the Beam Receiver and add an action to On Signal High ().Select
For the
...
object
...
select the
...
Revolution Counter
...
GameObject
For the method, select RevolutionCounter.CountRevolution
To see if everything is working, activate Play Mode in Unity.
...