How to create a dance floor?

In this tutorial, you will learn how to create a dance floor.
When you get on the dance floor, you start dancing, when you leave the dance floor, you stop dancing. Is it very easy? Yes! It only takes a few lines of code to create this experience.

1936

Let's start now! Please follow the steps below.

Step 1: Create a new scene with Para World Root

Open Unity, and create a new URP project. Then, open this project. Create a new URP scene as shown in the figure below.

1004

new scene in the menu

Place a spawn point with a Para World Root component.

1750

Step 2: Create the dance floor area

  1. Create a Plane in the scene as the floor.
970
2090

This is the plane you can walk on it.

  1. Create an empty object in the scene, and rename it to Dance Floor.
646
  1. Add a Box Collider component to the Dance Floor, and set the Is Trigger property to true.
860
  1. Click the Edit Collider button, and resize the collider to large enough for players to dance in.
868 1832
  1. Move it to a location you want.
3456

The dance floor area

Step 3: Attaching a script

Code in ParaSpace is written in a language called Lua, and it's stored and run from scripts. You can attach scripts to objects, and Unity will run the code in the script.
Now, create a new script, and rename it to DanceFloor. Then, attach it to the Dance Floor GameObject.
See the detail steps in Creating and Using Scripts as shown in the figure below.

1872

Step 4: First variable

It's a good idea to start off your script by making a variable. A variable is a name associated with a value. Once a variable is created, it can be used again and again. Same as other Unity components, public variables can also be displayed in the [Inspector] window.
Copy the following code to create a variable for the dance animation.

*Learn more about variables: Variables and the Inspector

---@var DanceAnimation:UnityEngine.AnimationClip;
868

variables can be displayed in the [Inspector]

Step 5: Set the dance animation

Drag the animation to blank behind the DanceAnimation.
You can find a sample dance animation in the SDK: ParaWorldSDK/Examples/ParaPlayAnimation/

1576

Step 6: Dance function

When you get on the dance floor, you start dancing, when you leave the dance floor, you stop dancing.

-- Fired when a player's capsule enters the collider.
function OnPlayerTriggerEnter(player)
  	-- start dance
    player:PlayAnimation(DanceAnimation);
end

-- Fired when a player's capsule exits the collider.
function OnPlayerTriggerExit(player)
  	-- stop dance
    player:StopAnimation();
end

The code for the dance floor is now complete!