As an excellent game development platform, Unity 3D integrated development environment provides an excellent management mode, namely Physics Manager.
The physics manager manages the parameters of the physical effects in the project, such as the object's gravity, rebound force, speed, and angular velocity.
Execute the Edit→Project Settings→Physics command in Unity 3D to open the physics manager, as shown in the figure below.
You can change the physical effects in the game by adjusting the parameters in the physics manager as needed. The specific parameters are shown in the following table.
Parameter | Meaning | Features |
---|---|---|
Gravity | gravity | Applies to all rigid bodies, generally only works on the Y axis |
Default Material | Default physical material | If a collider has no physical material set, the default material will be used |
Bounce Threshold | Rebound threshold | If the relative speed of the two colliding bodies is lower than this value, they will not bounce |
Sleep Velocity | Sleep speed | Objects below this speed will go to sleep |
Sleep Angular Velocity | Dormant angular velocity | Objects below this angular velocity will go to sleep |
Max Angular Velocity | Maximum angular velocity | Used to limit the angular velocity of a rigid body to avoid numerical instability during rotation |
Min Penetration For Penalty | Minimum penetration | Set how far they can penetrate before the collision detector separates two objects |
Solver Iteration Count | Number of iterations | Determines the calculation accuracy of joints and connections |
Raycasts Hit Triggers | Ray detection hit trigger | If this function is enabled, a hit message will be returned when the collision body is hit during ray detection; if this function is disabled, no hit message will be returned |
Layer Collision Matrix | Layer collision matrix | Define the behavior of the layer collision detection system |
Comprehensive case: the maze
Case Idea
Labyrinth treasure hunting games are a common type of game. Players can find treasure chests in the maze within a limited time to achieve the goal of clearance.
This project aims to virtual roam through the scene, find the hidden treasure chest in the maze, and collect it when approaching the treasure chest to realize the collision detection function.
Case design
In this case, a three-dimensional maze scene is created in Unity 3D. There are several treasure chests scattered in the scene. The game player needs to find the treasure chest within the specified time, approach and collect it, and finally clear the level.
Project implementation
1) Build a maze scene
Step 1): Create a new project and name the scene migong.
Step 2): Create game objects. Execute the GameObject→3D Object→Plane command in the menu bar to create a plane and assign materials.
Execute the GameObject→3D Object→Cube command to create several boxes to form a maze scene, as shown in the figure below.
Step 3): Import model resources. Select the 3D model resource from the Unity 3D store and load it into the scene, and name it treasure, as shown in the figure below.
Step 4): Import the model resources into the Hierarchy view, as shown in the figure below.
Step 5): Execute the Assets→Import Package→Custom Package command to add first-person resources, as shown in the figure below.
Step 6): After selecting the first-person resource, click the Import button to import the resource, as shown in the figure below.
Step 7): Search for the first person controller in the Project view, add it to the Hierarchy view, and place it in a suitable position on the plane, as shown in the figure below.
Step 8): Because the first person resource comes with a camera, the camera in the scene needs to be turned off.
2) Add trigger
Step 9): Select treasure, add Box Collider to treasure object, and check Is Trigger property, as shown in the figure below.
Step 10): Write the script Triggers.cs, the code is as follows.
using UnityEngine;using System.Collections;public class Triggers:MonoBehaviour{ void OnTriggerEnter(Collider other){ if(other.tag=="Pickup"){ Destroy(other.gameObject); } }}
Step 11): Link the Triggers script to the first person controller.
Step 12): Add the tag Pickup to treasure.
3) Add counting function
Step 13): Modify the script.
using UnityEngine;using System.Collections;public class Triggers:MonoBehaviour{ public static int temp_Num=0; void OnTriggerEnter(Collider other){ if(other.tag=="Pickup"){ temp_Num++; Destroy(other.gameObject); } } void OnGUI(){ if(temp_Num==5) if(GUI.Button(new Rect(Screen.width/2f, Screen.height/2f, 100, 50),"play again")){ temp_Num=0; Application.LoadLevel("migong"); } }}
步骤 14):将场景添加到 Build Settings 中,如下图所示。
4) 添加计时功能
步骤 15):完善代码,如下所示。
using UnityEngine;using System.Collections;public class Triggers:MonoBehaviour{ public static int temp_Num=0; public int parachuteNum; int timer; int time_T; bool isWin=false; bool isLose=false; void Start(){ Time.timeScale=1; GameObject[]objs=GameObject.FindGameObjectsWithTag("Pickup"); parachuteNum=objs.Length; time_T=(int)Time.time; } void Update(){ timer=20-(int)Time.time+time_T; if(temp_Num==parachuteNum&&timer!=0){ isWin=true; } if(timer==0&&temp_Num!=parachuteNum){ isLose=true; } } void OnTriggerEnter(Collider other){ if(other.tag=="Pickup"){ temp_Num++; Destroy(other.gameObject); } } void OnGUI(){ GUI.Label(new Rect(0, 0, 100, 50), timer.ToString()); if(isWin==true||isLose==true){ Time.timeScale=0; if(GUI.Button(new Rect(Screen.width/2f, Screen.height/2f, 100, 50), "play again")){ isWin=false; isLose=false; temp_Num=0; Application.LoadLevel("migong"); } } }}
Step 16): Click the Play button to test, the effect is shown in the figure below.