Creating Controllable Character
Let's create a playable robot that can move around the Play Area and collide with objects or wall obstacles. Our character will fly above the floor and rotate to face the cursor.
Step 1. Engage Physics for Robot's Model#
The robot with a complex 3D model will represent the playable character in the game. We have already imported the Node Reference with the skinned mesh, the flying animation, and materials for the robot.
The robot must be able to move around the Play Area and collide with static and physical objects. In order to do so, it should have a physical body and a collision shape approximating its volume.
-
Place the imported programming_quick_start\character\robot\robot.node on the floor inside the Play Area by dragging it from the Asset Browser directly to the Editor Viewport.
-
With the robot node selected click Edit in the Reference section of the Parameters window. The Node Reference shall open and the current selection shall focus on the robot ObjectMeshSkinned node. Now switch to the Physics tab of the Parameters window and assign a Rigid body to the selected ObjectMeshSkinned.
- Set LDamping parameter to 5.0 to make sure that the robot will lose speed over time.
-
Scroll down to the Shapes section and add a Capsule shape to the body.
The capsule shape will be used as an approximation volume for collisions with other objects in the world.
Step 2. Set Up Controls#
We will apply a linear impulse to the body to move the robot with keyboard WASD keys. The robot's motion will be determined according to the camera's orientation. Also, let's restrict the physics-based rotation and vertical movement to avoid unwanted control behavior.
To orient the robot to face the cursor, we will use one of the intersection types called World Intersection. It will trace a line from the cursor position to the floor to get an intersection point that will be used as a reference for the robot's rotation. You can read more about managing various intersections here.
The best way to manage keyboard and mouse inputs, is to use the Input class. It enables you to check the state of the buttons and get the current mouse coordinates. Alternative methods for input handling are described here.
Let's use the C# Component System to implement this logic. We will create a C# component and assign it to the robot's node in the world.
-
In the Asset Browser choose Create->Create C# Component and call it PlayerController.
-
Select the robot node (ObjectMeshSkinned), then click Add New Component or Property in the Parameters window and assign the PlayerController component.
- Open your IDE to edit the component by double-clicking on the created PlayerController.cs in the Asset Browser and сopy the code below.
It is recommended to use VS Code as a default IDE.
public class PlayerController : Component { Player player; BodyRigid rigid; vec3 pos; // a WorldIntersection object to store the information about the intersection WorldIntersection intersection = new WorldIntersection(); void Init() { player = Game.Player; rigid = node.ObjectBodyRigid; rigid.AngularScale = new vec3(0.0f, 0.0f, 0.0f); // restricting the rotation rigid.LinearScale = new vec3(1.0f, 1.0f, 0.0f); // restricting Z movement rigid.MaxLinearVelocity = 8.0f; // clamping the max linear velocity } void UpdatePhysics() { if (!Unigine.Console.Active) // do not process input if the console is shown { // check if W key is pressed if (Input.IsKeyPressed(Input.KEY.W)) Move(player.GetWorldDirection(MathLib.AXIS.Y)); // move forward // check if S key is pressed if (Input.IsKeyPressed(Input.KEY.S)) Move(player.GetWorldDirection(MathLib.AXIS.NY)); // move backward // check if A key is pressed if (Input.IsKeyPressed(Input.KEY.A)) Move(player.GetWorldDirection(MathLib.AXIS.NX)); // move left // check if D key is pressed if (Input.IsKeyPressed(Input.KEY.D)) Move(player.GetWorldDirection(MathLib.AXIS.X)); // move right // finding the positions of the cursor and the point moved 100 units away in the camera forward direction ivec2 mouse = Input.MouseCoord; vec3 p0 = player.WorldPosition; vec3 p1 = p0 + new vec3(player.GetDirectionFromScreen(mouse.x, mouse.y)) * 100; // casting a ray from p0 to p1 to find the first intersected object Unigine.Object obj = World.GetIntersection(p0, p1, 1, intersection); // the first bit of the intersection mask is set to 1, the rest are 0s // finding the intersection position, creating a transformation matrix to face this position and setting the transform matrix for the body preserving current angular and linear velocities if (obj) { pos = intersection.Point; pos.z = rigid.Transform.Translate.z; // project the position vector to the Body Rigid pivot plane mat4 transform = MathLib.SetTo(rigid.Transform.Translate, pos, vec3.UP, MathLib.AXIS.Y); rigid.SetPreserveTransform(transform); // turn the character's body } } } // moving the rigid body with linear impulse in the specified direction void Move(vec3 direction) { // direction is a normalized camera axis vector rigid.AddLinearImpulse(direction); } }
You can copy this sample code and paste it to your source files, however, be careful not to change the values of the default [Component(PropertyGuid = "")] attributes. - Return to the UnigineEditor. The PlayerController component will be automatically reimported, since it just has been changed. After the Reimport process, you will receive a prompt message, indicating the results of the C# Component System compilation.
If you encounter the red error message in the Editor, indicating that some of the C# components were not built, check the Editor's Console window for details.
- Press Ctrl+S to save the world and the robot's Node Reference.
Step 3. Finalize and Run#
- Turn off the Intersection detection for every surface of the robot node to ignore intersections with the robot's own surfaces, as we do not want it in our robot's rotation implementation.
- For every wall object in the world, go to the Parameters window and find the Surfaces section in the Node tab. Select the box surface of the mesh and open an Intersection Mask window. Uncheck the first Intersection Mask 0 bit to make sure that walls will not affect the player's character turning.
- To make the cursor always visible, go to Windows->Settings->Controls section, change the Mouse Handle mode to User. You can also control the cursor via API.
- Save changes to the world and the robot via File->Save World (or press Ctrl+S hotkey).
Now build and run the game in the UnigineEditor to try out the robot's controls.