Car Door with Behaviour Model
In depth learning on Scene Configuration can be found in this course in Motive Academy.
This recipe uses the vehicle asset from the HRDP Auto Showroom Template available from Unity.
Setup
It’s always useful to make a copy of the original Prefab when you are going to make changes. We generally recommend using Prefab Variants. In this case, with the Scene open, drag the XC40_Recharge Prefab object from your Scene into a new folder called “Props.” You will be asked whether you want to create a Variant.
1. Choose Prefab Variant:
Let’s make a couple of changes to this Prefab:
Disable the Collider object. These Colliders interfere with the door Colliders which prevents the Grabbable and Hinge Joint from working properly:
Disable the Car Door script on the base of the prefab. This Script takes over control of the door rotation which prevents the door from opening:
Step 1: Make Door_Front_LH Grabbable
Right-click and choose Make Grabbable from the Motive menu:
Step 2: Add Hinge Joint
Motive > Joints > Add Hinge Joint. Note that the door rotates around the Y axis
Scroll down to the Hinge Joint component and adjust the axes:
Note that both Axis and Secondary Axis need to be updated since these two axes need to be perpendicular to each other.
Next let’s take care of the warning on the Hinge Joint. The joint needs to connect to another Rigidbody.
Add a Rigidbody to the Body node
Mark the Rigidbody as Kinematic:
Then connect this Rigidbody to “Connected Rigidbody” field on the Hinge Joint:
Step 3: Add a Handle
The final step for this phase is to add a handle so that the physics works properly.
Add an empty object to Door_Front_LH and rename it Handle
Drag it into position so that it lines up with the handle:
Now add a Capsule Collider to the handle to make it interactible:
And finally for the Grabbable, make this Handle the Grab Point:
Step 4: Add Hinge Limits
Now you should be able to enter the scene and grab the door by the handle. The door will swing on the hinge, but you’ll notice that it can swing fully around the hinge with no limits. We can add limits to the Hinge Joint to make the door open and close properly:
Note that the Lower/Upper rotation is determined by the Axis and Secondary Axis. The Hinge starts at Lower Rotation.
Step 5: Effects
Haptic feedback is important for these sorts of mechanical interactions. You can quickly add haptics to this movement by adding a Joint Effects component (see Joint Effects.)
The “Bump” effect is included in the SDK.
Step 6: Behaviour Model
As a final step, we can add a Behaviour Model to the door so that its state can be tracked in Storyflow. For more information about using Behaviour Models and Enums, click here. You can also learn more about Enums here.
Make sure the door has a Motive Scene Object so that we can use it in Storyflow:
We’ll track the Position as a value that can be in the Closed, Ajar, and Open states:
We can use a Behaviour Model with an Enum state in Storyflow to track these states:
Usage example
Activate a Behaviour Model Binding targeted at “Driver Side Front Door.” Use the shortcut to create a “Door Position” variable:
Use a Variable Value Condition to check for the door being open:
When you run this Script, the Screen Message should pop up when the user opens the door.