【游戏开发引擎】 实验1:复杂地形穿越


前言

游戏开发引擎 实验1:复杂地形穿越


一、实验目的

  1. 怎样设计基本的游戏;
  2. 怎样应用地形知识构建特定于游戏的世界;
  3. 怎样向游戏中添加对象以提供交互性;
  4. 怎样测试和调整完成的游戏。

  在本实验中,你将消化迄今为止所学的知识,并使用它们构建你的第一个 Unity 游戏。 你首先将了解游戏的基本设计元素。接着,将构建游戏发生的世界。然后,将添加一些交互 性对象,以使玩家能够玩游戏。最后,将开始玩游戏,并执行任何必要的调整以改进体验。

二、实验环境

  1. 操作系统:WINDOWS 10
  2. 开发工具:UNITY
  3. 实验设备:PC

三、实验内容

  自己搭建一个有障碍或陷阱等元素的地形,有玩家在地形中穿越的出发点及终点,在穿越过程中碰到障碍或掉入陷阱则视为穿越失败一次,玩家自动回到出发点重新开始穿越。失败次数超过某个设定值或到达终点,都结束一次游戏过程,相应显示成功或失败的相关统计信息(如用时、失败次数等式)并可重新开始新的一次游戏。玩家可以是第一人称或第三人称的角色控制器。加入必要游戏对象、模型及编写有关脚本等,能通过键盘和鼠标相结合进行穿越。可根据需要增加其他内容。

1.创建游戏世界

在这里插入图片描述

1.1雕刻游戏世界

(1)在名为 Amazing Racer 的文件夹中创建一个新项目,并向该项目中添加一个地形。
(2)把该地形的分辨率设置为 200(宽)×100(长)×100(高)(在 Terrain Settings 的
Resolution 区域中设置它)。
(3)导入terrain.raw文件作为地形的高度图(通过在 Terrain Settings 的 Heightmap 区域中单击 Import Raw 命令)。
(4)在资源下面创建一个 Scenes 文件夹,并把当前场景另存为 Main。
在这里插入图片描述

1.2添加环境

导入需要的程序包(单击Assets > Import Package)
(1)向地形中添加一些树木;
(2)向场景中添加一些基本的水;
(3)向场景中水边添加草丛。
在这里插入图片描述

2.角色控制器

(1)单击 Assets > Import Package > Character Controller 命令,导入标准的角色控制器。
(2)从 AssetsCharacter Controllers 文件夹中把 First Person 控制器资源拖入场景中。
(3)把 First Person 控制器(它将被命名为 Player 并设置为蓝色)放置在(160, 32, 64)处。
在 y 轴上把控制器旋转 260°,使之面朝正确的方向。
在这里插入图片描述
在这里插入图片描述

3.添加游戏控制对象

复活点:
(1)向场景中添加一个空的游戏对象(单击 GameObject > Create Empty 命令),并把它定位
于(160, 32, 64)处。
(2)在 Hierarchy 视图中把空对象重命名为 SpawnPoint。
在这里插入图片描述
检测器:
(1)向场景中添加一个平面(单击 GameObject > Create Other > Plane 命令),把它定位于(86,
27, 51)处,并把该平面缩放为(10, 1, 10)。
(2)在 Hierarchy 视图中把平面重命名为 WaterHazardDetector。
(3)在 Inspector 视图中选中 Mesh Collider 组件上的 Is Trigger 复选框
在这里插入图片描述
完成区域:
(1)向场景中添中一个空的游戏对象,并把它定位于(26, 32, 24)处。
(2)在 Hierarchy 视图中把该对象重命名为 Finish。
(3)给完成区域对象添加一个灯光组件(选取该对象,单击 Component > Rendering > Light 命令)。如果它的类型还不是 Point,就把类型更改为 Point,并把范围和强度分别设置为 35 和 3。
(4)选取完成区域对象并单击 Component > Physics > Capsule Collider 命令,给该对象添加 一个胶囊碰撞器。在 Inspector 视图中把 Radius 属性改为 9,并选中 IsTrigger 复选框
在这里插入图片描述
游戏控制对象:
(1)向场景中添加一个空的游戏对象。
(2)在 Hierarchy 视图中把该游戏对象重命名为 GameControl。
在这里插入图片描述

4.添加脚本和把脚本连接在一起

(1)对 Finish 游戏对象应用 FinishScript。
在这里插入图片描述
FinishScript代码如下:

FinishScript.cs:
using UnityEngine;
using System.Collections;

public class FinishScript : MonoBehaviour {
	
	//This is a place holder for the script that controls the game
	public GameControlScript gameControlScript;
	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	}
	
	//This states that when an object enters the finish zone, let the
	//game control know that the current game has ended
	void OnTriggerEnter(Collider other)
	{
		gameControlScript.FinishedGame();
	}
}

(2)对 GameControl 对象应用 GameControlScript。
在这里插入图片描述
GameControlScript代码如下:

GameControlScript.cs:
using UnityEngine;
using System.Collections;

public class GameControlScript : MonoBehaviour {
	
	//The amount of ellapsed time
	private float time = 0;
	
	//Flags that control the state of the game
	private bool isRunning = false;
	private bool isFinished = false;
	
	//place holders for the player and the spawn point
	public Transform spawnPoint;
	public GameObject player;
	
	//place holders for the scripts on the character controller
	public CharacterMotor motorScript;
	public MouseLook lookScript;
	
	//This resets to game back to the way it started
	private void InitLevel()
	{
		time = 0;
		isRunning = true;
		isFinished = false;
		
		//move the player to the spawn point
		player.transform.position = spawnPoint.position;
		
		//Allow the character controller to move and
		//look around
		motorScript.enabled = true;
		lookScript.enabled = true;
	}
	
	// Use this for initialization
	void Start () {
		
		//prevent the character controller
		//from looking around
		motorScript.enabled = false;
		lookScript.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {
		
		//add time to the clock if the game
		//is running
		if(isRunning)
			time += Time.deltaTime;
	}
	
	//This runs when the player enters the finish
	//zone
	public void FinishedGame()
	{
		isRunning = false;
		isFinished = true;
		
		//freeze the character controller
		motorScript.enabled = false;
		lookScript.enabled = false;
	}
	
	//This section creates the Graphical User Interface (GUI)
	void OnGUI()
	{
		
		if(!isRunning)
		{
			string message;
			if(isFinished)
				message = "Click to Play Again";
			else
				message = "Click to Play";
			
			if(GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height/2, 140, 30), message))
			{
				//start the game if the user clicks to play
				InitLevel ();
			}
		}
		
		//If the player finished the game, show the final time
		if(isFinished)
		{
			GUI.Box(new Rect(Screen.width / 2 - 65, 185, 130, 40), "Your Time Was");
			GUI.Label(new Rect(Screen.width / 2 - 10, 200, 20, 30), ((int)time).ToString());
		}
		//If the game is running, show the current time
		else if(isRunning)
		{
			GUI.Box(new Rect(Screen.width / 2 - 65, Screen.height - 115, 130, 40), "Your Time Is");
			GUI.Label(new Rect(Screen.width / 2 - 10, Screen.height - 100, 20, 30), ((int)time).ToString());
		}
		
	}
}

(3)对 WaterHazardDetector 对象应用 RespawnScript。
在这里插入图片描述
RespawnScript代码如下:

RespawnScript.cs:
using UnityEngine;
using System.Collections;

public class RespawnScript : MonoBehaviour {
	
	//Place holder for the spawn point
	public Transform respawnPoint;
	// Use this for initialization
	void Start () {	
	}
	
	// Update is called once per frame
	void Update () {
	}
	
	//This fires off when the player enters
	//the water hazard
	void OnTriggerEnter(Collider other)
	{
		//Moves the player to the spawn point
		other.gameObject.transform.position = respawnPoint.position;
	}
}

在这里插入图片描述

5.测试游戏

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


总结

术语
  本实验中使用了下面一些新术语。
    复活:复活是一个过程,玩家或实体通过它进入游戏。
    复活点:复活点是玩家或实体复活的位置,游戏中可以有一个或多个复活点,它们可以是静止的,或者是四处移动的。
    条件:条件是一种触发器形式。获胜条件是使玩家赢得游戏的事件(比如积累足够的点 数);失败条件是使玩家输掉游戏的事件(比如丢失所有的单击点数)。
    游戏控制器:游戏控制器规定了游戏的规则和流程。它负责知道何时赢得或输掉游戏(或 者只是游戏结束了)。可以把任何对象指定为游戏控制器,只要它总是在场景中即可。通常,把一个空对象或者 Main Camera 指定为游戏控制器。
    脚本:GameObject的行为都是被附加到其上面的组件控制,脚本本质上也是一个组件。

  在unity中创建一个脚本 ,脚本通过实现一个派生自”MonoBehaviour”的类来与unity的内部工作机制建立联系。可以将新创建的组件类型的类作为一个蓝图,该类作为一个新类型的组件被附加到游戏对象。每次将一个脚本组件附加到游戏对象,都会创建一个该蓝图定义的对象的实例。创建的脚本文件的文件名必须与里面的类名相同,这样才能将其附加到游戏对象上。
  Update函数处理游戏对象的帧更新相关的操作。可能包括移动,触发动作以及对用户输入的反馈,基本上在游戏过程期间需要处理的任何事情都可以在这里面处理。Start函数在游戏开始前被unity调用(例如,在Update被第一次调用之前),因而是一个进行初始化操作的理想位置。为什么不把初始化操作放在类的构造函数中,这是因为对象的构造是由编辑器处理的,在游戏开始的时候并不如想象中那样会发生。如果为一个脚本组件定义构造器,它会和unity的正常操作发生干扰从而导致一些问题。脚本被创建之后是处于不激活状态的,只有将它的一个实例附加到一个游戏对象之后代码才会被激活。同时,一个游戏对象的一个类型的组件只能有一个,也就是可以附加到游戏对象的脚本组件不能超过一个。
  在本实验中,我在 Unity 中制作了自己的第一款游戏。首先设计了游戏理念、规则和需求的多个方面。接着构建了游戏世界,并且添加了环境效果。然后添加了交互性所需的游戏对象。对那些游戏对象应用了脚本,并把它们联系在一起。最后对游戏进行了测试,并且注明了喜欢和不喜欢的事情。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>