Unity VR Open Close Animated Door, toggle animations

Scripts written for you by Alex Rickett, UCLA Game lab. These SteamVR scripts will open an animated door and reverse to close, useful for many events, such as turning on lights. This works with SteamVR.

In Maya
Animate a door opening and save to your Unity Assets folder.

Make sure the Pivot Center is located on the hinge point.

Download these script files:  (These scripts do not need SteamVR.)
Place the scripts into your Assets folder
     BackAndForthAnimation.cs and ClickEventsHelper.cs

In Unity:

Setup the Animation
Select your Maya Model in the Assets area

Switch to the Rig tab in the Inspector on the right.
     Change Animation Type to Legacy  (default was Generic)
     Hit Apply

Change to the Animation section of the Inspector tab
     Change the Clip Name to Take 001 + something descriptive

Hit Apply

(You may use either Legacy Animation or Default Animation.)

Setup the Model
Drag the animated model from your Assets to your Scene Hierarchy

Select the model in the Hierarchy
     In the Inspector, uncheck Play Automatically

Select the animated model in the
     Add Component->Physics->Box Collider      (Any collider will work.)

Drag the two scripts onto your model, one at a time.

In the Inspector add the name of the Clip to the Back And Forth Animation script
     If the clip is from Maya you named it Take 001 + something descriptive.

In the Inspector, select the + to add an event to the Click Events Helper script
     Add the model to the box that reads None (Object)
    Change No Function to BackAndForthAnimation->ToggleOpen


Now run the game your door will open and close when you click on it.
This works with anything animated you want to start and stop.
You can assign other objects to toggle your animations such as a light switch and a light.

Add a Sound Effect
Select the animated model in the Hierarchy

     Add Component->Audio Source in the Inspector
     Add an AudioClip
     Turn off Play on Awake
In the Inspector, select the + to add an event to the Click Events Helper script
     Add the model to the box that reads None (Object)
    Change No Function to AudioSource->Play ()

VR: add Hover Events to objects with the Door Open Scripts
Click events only works with mouse clicks for the simulator.
The animated object uses a collider.

Use Add Component

     Search for inter
     Select Interactable Hover Events

Hit the + under On Hand Hover Begin () in the Inspector

Drag and drop Maya Model from the Hierarchy onto None (obj) in the Inspector

Click the drop down menu reading No function

     Choose BackAndForthAnimation->ToggleOpen

Interactable Script in the Inspector
     Turn off Highlight on Hover

You may also add a Hover event to start audio
I noticed the sound plays more immediately if you turn off the Interactable Script.

If you use Hover Events, you can turn off the Click events script or leave it as is.

Probem Solving
If you have a mesh collider on the wall that has a door, you may need to play with turning on/off Convex.



© 1988-2024 36 years