Basic Tutorial - Revolution Counter
- 1 Project goal
- 2 Project overview
- 3 Step 1. Create a Kinematics Controller
- 4 Step 2. Add a Wheel Joint to the Axis object
- 5 Step 3. Add a DCMotor to the Motor object
- 6 Step 4. Add a Emitter to the Emitter Lens Object
- 7 Step 5. Add a Receiver to the Receiver Eye Object
- 8 Step 6. Have the Beam Receiver execute an action when the laser is detected.
To get started, first download the Tutorial Project
Project goal
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.
You can find the tutorial scene in the Tutorial Project under
Assets > 1 - Basic Tutorials > 2 - Revolution Counter Tutorial > 1 - Exercise - Revolution Counter Tutorial
3d-model of the Revolution Counter (supplied with the tutorial)
Hierarchy setup (supplied with the tutorial)
For reference, the finished setup has also been provided. Open the scene: 2 - Finished - Revolution Counter Tutorial under Assets > 1 - Basic Tutorials > 2 - Revolution Counter Tutorial > 1 - Exercise - Revolution Counter Tutorial
Step 1. Create a Kinematics Controller
If you want to use Kinematics, you will firstly need a V2020.1.56.3_ Kinematics Controller . This component controls all Kinematic Transforms in the scene.
To create a Kinematics Controller, go to Mechanics > Add Kinematics > Kinematics Controller.
The Kinematics Controller will be assigned to a selected GameObject, or when there is no GameObject selected, a new one will be automatically created.
For now, deselect everything and add a Kinematics Controller.
Make sure to parent the Kinematic Controller above the parts you want to control, in this case the Revolution Counter. Drag the Revolution Counter onto the NEW_KINEMATICSCONTROLLER.
Step 2. Add a Wheel Joint to the Axis object
To create a V2020.1.56.3_ Wheel Joint and assign it to a GameObject, select the GameObject and go to Mechanics > Add Kinematics > Wheel Joint.
This will create a Wheel Joint attached to the Axis. Make sure your Gizmos are turned on, so will you see the newly created Wheel Joint. To turn on Gizmos, go to the top-right of the Scene View, and press Gizmos.
The Direction and Radius of the Wheel Joint are not correct, we will change the Wheel Axis Direction > Vector to Forward and the Wheel Radius to 0.02.
Step 3. Add a DCMotor to the Motor object
To create a DC Motor and assign it to a GameObject, select the GameObject and go to Standard Components > Motors > DCMotor > Add Physical Component.
Check if the correct Wheel Joint is applied to the DC Motor. Go to the Properties tab of the DC Motor and check whether the Wheel Joint is the correct one. Here you can also set the other parameters to your desired values.
Go to the Control Panel tab and Generate Control Panel For DCMotor. This enables the user to control the component from the Unity scene. Set the Preferred Velocity in the Control Panel to 200.
If you would like, you could now go to Play Mode and test whether the DC Motor is working and the flywheel starts spinning.
Step 4. Add a Emitter to the Emitter Lens Object
To create a V2020.1.56.3_ Emitter and assign it to a GameObject, select the GameObject and go to Standard Components > Beam Sensor > Emitter > Add Physical Component.
We will change some values of the Emitter so its facing the right direction and has the desired radius. Go to the Properties tab of the Emitter and set the Origin Direction to X:-1 Y:0 Z:0.
Step 5. Add a Receiver to the Receiver Eye Object
To create a V2020.1.56.3_ Receiver 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.
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 ().
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.
Prespective Documentation