Unity3D实现立体迷宫寻宝

这个小游戏是一个白痴在一个昏暗的房间走动找到关键得分点,然后通关游戏。入门Unity3D做的第一款游戏,比较无聊,但实现了一般的游戏功能。如,人物控制,碰撞检测,主控制器等。

游戏界面



控制代码

GameManager.cs

主控制脚本:用于控制整个游戏的主逻辑,屏幕显示一些提示字符以及游戏分数,并且根据游戏逻辑更新数值。同时,检测按键是否需要退出。

using UnityEngine;
using System.Collections; [AddComponentMenu("Game/GameManager")]
public class GameManager : MonoBehaviour {
public static GameManager Instance = null; // 游戏得分
public int m_score = 0;
// 游戏主角
Player m_player; // UI文字
GUIText txt_hiscore;
GUIText txt_score;
GUIText txt_win; // 初始化
void Start () {
Instance = this;
// 获得主角
m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); // 获得设置的UI文字
txt_score = this.transform.FindChild("txt_score").GetComponent<GUIText>();
txt_win = this.transform.FindChild("txt_win").GetComponent<GUIText>();
}
// 游戏胜利
public void setWin(){
txt_win.gameObject.SetActive (true);
m_player.enabled = false;
}
// 退出游戏
void Update(){
if (Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
}
// 更新分数
public void SetScore(int score){
m_score+= score;
txt_score.text = "Score "+m_score;
}
}

ItemHit.cs

碰撞检测脚本:碰撞得分+1,如果是最后一个得分点,则标识游戏胜利。

using UnityEngine;
using System.Collections; public class ItemHit : MonoBehaviour {
// Use this for initialization
void Start () { } void OnTriggerEnter(Collider other) {
//判断palyer对象是否和得分点接触
if( other.tag == "Player" ){
GameObject.Destroy( this.gameObject );
GameManager.Instance.SetScore(1);
//判断全部得分点都已经过,结束游戏,打印win
if( GameObject.FindObjectsOfType<ItemHit>().Length == 1 ){
GameManager.Instance.setWin();
}
}
}
}

player.cs

人物控制脚本:在这里可以控制对象的一些属性,例如重力数值,移动速度,摄像机参数,初始生命值。

Start( )的时候需要绑定对象;

Update( )的时候需要更新人物位置,并且让小摄像机追踪人物,小摄像机用于小地图显示人物当前位置。

using UnityEngine;
using System.Collections; [AddComponentMenu("Game/Player")]
public class Player : MonoBehaviour { // 组件
public Transform m_transform;
CharacterController m_ch; // 角色移动速度
float m_movSpeed = 10.0f; // 重力
float m_gravity = 2.0f; // 摄像机
Transform m_camTransform; // 摄像机旋转角度
Vector3 m_camRot; // 摄像机高度
float m_camHeight = 1.4f; // 生命值
public int m_life = 5; void Start () { // 获取组件
m_transform = this.transform;
m_ch = this.GetComponent<CharacterController>(); // 获取摄像机
m_camTransform = Camera.main.transform; // 设置摄像机初始位置
Vector3 pos = m_transform.position;
pos.y += m_camHeight;
m_camTransform.position = pos;
m_camTransform.rotation = m_transform.rotation; m_camRot = m_camTransform.eulerAngles; Screen.lockCursor = true; } void Update () {
Control();
} void Control(){
//获取鼠标移动距离
float rh = Input.GetAxis("Mouse X");
float rv = Input.GetAxis("Mouse Y"); // 旋转摄像机
m_camRot.x -= rv;
m_camRot.y += rh;
m_camTransform.eulerAngles = m_camRot; // 使主角的面向方向与摄像机一致
Vector3 camrot = m_camTransform.eulerAngles;
camrot.x = 0; camrot.z = 0;
m_transform.eulerAngles = camrot; float xm = 0, ym = 0, zm = 0; // 重力运动
ym -= m_gravity*Time.deltaTime; // 上下左右运动
if (Input.GetKey(KeyCode.W)){
zm += m_movSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.S)){
zm -= m_movSpeed * Time.deltaTime;
} if (Input.GetKey(KeyCode.A)){
xm -= m_movSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D)){
xm += m_movSpeed * Time.deltaTime;
}
//移动
m_ch.Move( m_transform.TransformDirection(new Vector3(xm, ym, zm)) ); // 使摄像机的位置与主角一致
Vector3 pos = m_transform.position;
pos.y += m_camHeight;
m_camTransform.position = pos;
}
}

完整工程

传送门:这里

Unity3D实现立体迷宫寻宝的更多相关文章

  1. nyoj 82 迷宫寻宝(一)

    点击打开链接 迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫 ...

  2. 迷宫寻宝(一)(bfs)

    迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫里有N个编 ...

  3. Problem 2285 迷宫寻宝 (BFS)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2285 Problem 2285 迷宫寻宝 Accept: 323    Submit: 1247Time Li ...

  4. 福州大学第十五届程序设计竞赛_重现赛B题迷宫寻宝

    Problem B 迷宫寻宝 Accept: 52    Submit: 183Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem De ...

  5. NYOJ82 迷宫寻宝(一)【BFS】

    迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 一个叫ACM的寻宝者找到了一个藏宝图.它依据藏宝图找到了一个迷宫,这是一个非常特别的迷宫,迷宫里有N个 ...

  6. Problem 2285 迷宫寻宝

    http://acm.fzu.edu.cn/problem.php?pid=2285 Problem Description 洪尼玛今天准备去寻宝,在一个n*n (n行, n列)的迷宫中,存在着一个入 ...

  7. nyoj 82 迷宫寻宝(二)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=83 题目解法主要在于判断两线段是否相交,思路是穷举所有地图四周的点,其中每一个边界上的点和终点构成一 ...

  8. 【DFS】NYOJ-82 迷宫寻宝(一)-条件迷宫问题

    [题目链接:NYOJ-82] #include<iostream> #include<cstring> using namespace std; struct node{ in ...

  9. nyist 82迷宫寻宝(一)(BFS)

    题目连接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 此题在基础BFS上加入了门和钥匙,要找齐所有钥匙才能开门,所以要对门特殊处理. 1.先 ...

随机推荐

  1. Arduino

    ========================================= Sites/Blogs http://arduino.cc/ http://www.geek-workshop.co ...

  2. sql语句将本地服务器中的数据插入到外网服务器中

    --将本地的数据库中的某张表中的数据导入到180的数据库中 --这个要在本地的数据库运行 exec sp_addlinkedserver 'srv_lnk', '', 'SQLOLEDB','xxx. ...

  3. SQL Server 2012入门图解:建表、备份、还原

    一.建立你的第一个数据库和表   例:建立一个用于描述一个学校学生情况的数据库.把它命名为School.并且要在School数据库下建立保存学生信息的表Student.在可视化界面下,我们通常这样操作 ...

  4. Developers, do consider different user roles! - A bad experience with cron

    The Story: Last week, I found one of our embedded arm linux device  ran out of flash space( totally ...

  5. Part 14 ng hide and ng show in AngularJS

    ng-hide and ng-show directives are used to control the visibility of the HTML elements. Let us under ...

  6. WPF非UI线程获取修改控件属性值的方法

    public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, ...

  7. 清除windows的EFS加密

        所使用软件为aefsdr_setup_en,搜索名为advanced.efs.data.recovery   1.         创建需要加密的文件   2.         进行加密   ...

  8. Objective-C中的封装、继承、多态、分类

    封装的好处: 过滤不合理的值 屏蔽内部的赋值过程 让外界不必关注内部的细节 继承的好处: 不改变原来模型的基础上,拓充方法 建立了类与类之间的联系 抽取了公共代码 坏处:耦合性强(当去掉一个父类,子类 ...

  9. Selenium定位元素

    Commands (命令) Action对当前状态进行操作失败时,停止测试 Assertion校验是否有产生正确的值 Element Locators指定HTML中的某元素 Patterns用于模式匹 ...

  10. 问题:LVM lvextend增加空间后,df查看还是原来空间

    1.LVM的调整空间大小: #lvextend -L +1300M /dev/mapper/ycgsstore_sdb-wmy #lvdisplay wmy ycgsstore_sdb -wi-ao- ...