播放背景音乐
The game must output some audio besides the bullet hit sound effect. To play the background music we will use the component system once again.游戏必须输出一些音频除了子弹击中音效。播放背景音乐,我们将再次使用的组件系统。
Let's create the node with a Music Player component that plays the looped music from the game start.让我们创建的节点Music Player组件从游戏开始,毛圈的音乐。
-
Create a new C# component and call it MusicPlayer. Open your IDE and copy the code below. Save your code in the IDE to ensure it's automatic compilation on switching back to UnigineEditor.创建一个新的c#组件和MusicPlayer调用它。打开你的IDE和复制下面的代码。保存在IDE中代码以确保它的自动切换回UnigineEditor编译。
using System; using System.Collections; using System.Collections.Generic; using Unigine; #if UNIGINE_DOUBLE using Vec3 = Unigine.dvec3; using Mat4 = Unigine.dmat4; using Scalar = System.Double; #else using Vec3 = Unigine.vec3; using Mat4 = Unigine.mat4; using Scalar = System.Single; #endif [Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component public class PlayerController : Component { //========================== NEW - BEGIN =============================== bool isNextLeft = false; // mouse fire button public Input.MOUSE_BUTTON mouseFireKey = Input.MOUSE_BUTTON.RIGHT; // asset file that contains the bullet public AssetLinkNode bullet; public Node leftSpawn, rightSpawn; //========================== NEW - END =============================== 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 } //========================== NEW - BEGIN =============================== void Update() { if (Input.IsMouseButtonDown(mouseFireKey) && bullet.IsFileExist) { // load the bullet and set its position if (isNextLeft) bullet.Load(rightSpawn.WorldTransform); else bullet.Load(leftSpawn.WorldTransform); // alternate between the left and the right gun isNextLeft = !isNextLeft; } // press ESC button to close the game if (Input.IsKeyDown(Input.KEY.ESC)) { Engine.Quit(); } } //========================== NEW - END =============================== void UpdatePhysics() { // forward if (Input.IsKeyPressed(Input.KEY.W)) Move(player.GetWorldDirection(MathLib.AXIS.Y)); // backward if (Input.IsKeyPressed(Input.KEY.S)) Move(player.GetWorldDirection(MathLib.AXIS.NY)); // left if (Input.IsKeyPressed(Input.KEY.A)) Move(player.GetWorldDirection(MathLib.AXIS.NX)); // right if (Input.IsKeyPressed(Input.KEY.D)) Move(player.GetWorldDirection(MathLib.AXIS.X)); // finding the positions of the cursor and the point moved 100 units away in the camera forward direction ivec2 mouse = Input.MousePosition; Vec3 p0 = player.WorldPosition; Vec3 p1 = p0 + new Vec3(player.GetDirectionFromMainWindow(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 transofrm matrix for the body preserving 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); } } // moving the rigid body with linear impulse in the specified direction void Move(vec3 direction) { //directon is a normalized camera axis vector rigid.AddLinearImpulse(direction); } }
- Create a new Dummy Node, rename it to "music_player", and place it somewhere in the world.创建一个新的Dummy Node,重命名它为"music_player",它在世界上的地位。
- Add the MusicPlayer component to the music_player node.MusicPlayer组件添加到music_player节点。
-
Assign the imported music asset (programming_quick_start/music/ost.mp3) to the Background Music field of the MusicPlayer component.指定导入的音乐资源(programming_quick_start/music/ost.mp3)的Background Music字段MusicPlayer组件。
- Save changes to the world, go to File->Save World or press Ctrl+S hotkey.拯救世界的变化,去File->Save World或者按Ctrl+S热键。
-
Run the project in the UnigineEditor to check out the background music.运行项目UnigineEditor检查背景音乐。