Unity3DGame学习笔记(4):射箭游戏
打靶游戏要求:
游戏演示:
![](/assets/blank.gif)
代码结构:
![](/assets/blank.gif)
设计思路与细节:
1.构建基本游戏对象:
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
2.射出箭矢:
if (Input.GetMouseButtonDown(0)) {Ray mouseRay = camera.ScreenPointToRay (Input.mousePosition);Debug.Log("RayDir = " + mouseRay.direction);Controller.shootArrow (mouseRay.direction);}
顺便,我还实现了箭头也能面向点击方向,只需设置箭的up或forward等为射线方向即可(具体根据箭对象的头是up还是forward设置)。
3.触发识别(ArrowScript与ScoreRecorder):
void OnTriggerEnter(Collider other) {;
if (bartNum == "") {
bartNum = other.gameObject.name;
Debug.Log ("arrowTrigger = " + bartNum); Destroy (GetComponent<Rigidbody>()); //使得箭停在靶子上
Component []comp = GetComponentsInChildren<CapsuleCollider>();
foreach (CapsuleCollider i in comp) {
i.enabled = false;
}
//使得箭不会触发靶子上的箭
GetComponent<MeshCollider>().isTrigger = false;
recorder.countScore (bartNum);
//记录分数
}
}
ScoreRecorder中相关代码如下:
public void countScore(string type) {if (type == "circle01")Score += 10;else if (type == "circle02")Score += 8;else if (type == "circle03")Score += 6;else if (type == "circle04")Score += 4;else if (type == "circle05")Score += 2;Debug.Log ("Score = " + Score);}
4.计时机制回收箭矢(ArrowScript和ArrowFactory):
private float time; //计时器void Update () {time += Time.deltaTime;if (time >= LIMITTIME) {this.gameObject.SetActive (false);initial ();//重置变量值}}
ArrowFactory中相关代码如下:
private void freeArrow() {for (int i = 0; i < UsedArrow.Count; i++) {GameObject temp = UsedArrow [i];if (!temp.activeInHierarchy) {UsedArrow.RemoveAt (i);FreeArrow.Add (temp);}}}void Update () {Debug.Log ("Used :" + UsedArrow.Count);Debug.Log ("Free :" + FreeArrow.Count);freeArrow ();}
完整代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class UserInterface : MonoBehaviour {private SceneController Controller;public Camera camera;public Text Score;public Text WindForce;public Text WindDirection;// Use this for initializationvoid Start () {Controller = (SceneController)FindObjectOfType(typeof(SceneController));Debug.Log ("Controller = "+ Controller);}// Update is called once per framevoid Update () {Score.text = "Score : " + Controller.getRecorder ().getScore (); //显示分数float force = Controller.getActionManager ().getWindForce ();if (force < 0) {WindDirection.text = "Wind Direction : Left";} else if (force > 0) {WindDirection.text = "Wind Direction : Right";} else {WindDirection.text = "Wind Direction : No Wind";}
//显示风向WindForce.text = "Wind Force : " + Controller.getActionManager ().getWindForce (); //显示风力if (Input.GetMouseButtonDown(0)) {Ray mouseRay = camera.ScreenPointToRay (Input.mousePosition);Debug.Log("RayDir = " + mouseRay.direction);Controller.shootArrow (mouseRay.direction);//以点击的方向发射箭矢}}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Director : System.Object {private static Director _instance;public SceneController Controller{ get; set;}public static Director getinstance() {if (_instance == null) {_instance = new Director ();}return _instance;}public int getFPS() {return Application.targetFrameRate;}public void setFPS(int fps) {Application.targetFrameRate = fps;}
}
3.SceneController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SceneController : MonoBehaviour {private ActionManager actionManager;private ScoreRecorder scoreRecorder;private ArrowFactory arrowFactory;void Awake() {Director director = Director.getinstance ();director.setFPS (60);director.Controller = this;actionManager = (ActionManager)FindObjectOfType (typeof(ActionManager));scoreRecorder = (ScoreRecorder)FindObjectOfType (typeof(ScoreRecorder));arrowFactory = (ArrowFactory)FindObjectOfType (typeof(ArrowFactory));Debug.Log("Controller");}public ArrowFactory getFactory() {return arrowFactory;}public ScoreRecorder getRecorder() {return scoreRecorder;}public ActionManager getActionManager() {return actionManager;}public void shootArrow(Vector3 dir) {GameObject arrow = arrowFactory.getArrow (); //获得箭矢arrow.transform.position = new Vector3(0, 2, 0); //设置箭矢初始位置actionManager.shoot (arrow, dir);//调用ActionManager实现动作细节}
}
4.ArrowFactory:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ArrowFactory : MonoBehaviour {private List<GameObject> UsedArrow; //储存使用中的箭矢private List<GameObject> FreeArrow; //储存空闲箭矢private GameObject arrowPrefab;void Awake() {arrowPrefab = Instantiate(Resources.Load ("Prefabs/arrow")) as GameObject;arrowPrefab.SetActive (false);FreeArrow = new List<GameObject> ();UsedArrow = new List<GameObject> ();Debug.Log ("ArrowFactory");}// Update is called once per framepublic GameObject getArrow() {GameObject temp;if (FreeArrow.Count == 0) {temp = GameObject.Instantiate (arrowPrefab) as GameObject;temp.SetActive (true);
//如果空闲箭矢中没有箭矢,就生成新的箭矢} else {temp = FreeArrow [0];temp.SetActive (true);if (temp.GetComponent<Rigidbody>() == null)temp.AddComponent<Rigidbody> ();Component []comp = temp.GetComponentsInChildren<CapsuleCollider>();foreach (CapsuleCollider i in comp) {i.enabled = true;}temp.GetComponent<MeshCollider>().isTrigger = true;Debug.Log ("temp = " + temp);FreeArrow.RemoveAt (0);
//空闲箭矢中有箭矢,初始化相关属性,加入使用}UsedArrow.Add (temp);return temp;}private void freeArrow() {for (int i = 0; i < UsedArrow.Count; i++) {GameObject temp = UsedArrow [i];if (!temp.activeInHierarchy) {UsedArrow.RemoveAt (i);FreeArrow.Add (temp);}}}void Update () {Debug.Log ("Used :" + UsedArrow.Count);Debug.Log ("Free :" + FreeArrow.Count);freeArrow ();}
}
5.ActionManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ActionManager : MonoBehaviour {private float speed = 40f; //箭矢初速private float Force = 0f; //第一箭风力默认为0void Awake() {Debug.Log ("ActionManager");}public void shoot(GameObject arrow, Vector3 dir) {arrow.transform.up = dir;//设置一下箭的朝向arrow.GetComponent<Rigidbody> ().velocity = dir * speed; //设置箭矢的速度。wind (arrow);Force = Random.Range(-100,100); //获取随机的风力}private void wind(GameObject arrow) {Debug.Log ("AddForce");arrow.GetComponent<Rigidbody> ().AddForce (new Vector3 (Force, 0, 0), ForceMode.Force); //对箭矢施加恒定的风力}public float getWindForce() {return Force;}
}
6.ScoreRecorder:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ScoreRecorder : MonoBehaviour {private int Score = 0;//初始分数为0public void countScore(string type) {if (type == "circle01")Score += 10;else if (type == "circle02")Score += 8;else if (type == "circle03")Score += 6;else if (type == "circle04")Score += 4;else if (type == "circle05")Score += 2;Debug.Log ("Score = " + Score);
//不同环不同分}public int getScore() {return Score;}
}
7.ArrowScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ArrowScript : MonoBehaviour {private string bartNum;private ScoreRecorder recorder;private float time; //计时器private static float LIMITTIME = 4;void initial() {time = 0;bartNum = "";}void OnTriggerEnter(Collider other) {;if (bartNum == "") {bartNum = other.gameObject.name;Debug.Log ("arrowTrigger = " + bartNum);Destroy (GetComponent<Rigidbody>()); //使得箭停在靶子上Component []comp = GetComponentsInChildren<CapsuleCollider>();foreach (CapsuleCollider i in comp) {i.enabled = false;}//使得箭不会触发靶子上的箭GetComponent<MeshCollider>().isTrigger = false;recorder.countScore (bartNum);//记录分数}}public string getBartNum() {return bartNum;}// Use this for initializationvoid Awake () {initial ();recorder = (ScoreRecorder)FindObjectOfType (typeof(ScoreRecorder));}// Update is called once per framevoid Update () {time += Time.deltaTime;if (time >= LIMITTIME) {this.gameObject.SetActive (false);initial ();//重置变量值}}
}
Unity3DGame学习笔记(4):射箭游戏相关推荐
- Unity学习笔记—二次元日系游戏制作(实践篇-游戏初始化场景制作)
原教程:siki:二次元日系游戏制作工具 - live2dSDK入门教程 http://www.sikiedu.com/my/course/282 (上)Unity学习笔记-二次元日系游戏制作(理论篇 ...
- [Unity学习笔记:FPS游戏制作(3)]子弹拖尾,碰撞与枪口火焰效果
往期博客[Unity学习笔记:FPS游戏制作(2)] 发射子弹----(2021.6.20学习笔记) 文章目录 一,实现思路 二,粒子效果的实现 (1)子弹拖尾特效的实现 (2)枪口火焰特效的实现 ( ...
- [Unity学习笔记:FPS游戏制作(2)] 发射子弹————(2021.6.20学习笔记)
往期博客 [Unity学习笔记:FPS游戏制作(1)]角色的移动,旋转与推进上升----(2021.6.13学习笔记) 文章目录 一,实现思路 二,实现代码 三,脚本的使用方法 四,最终效果 一,实现 ...
- Pygame学习笔记 6 —— 3D游戏
pygame是是上世纪的产品,虽然不适合最3D游戏,但我可以使用pygame来绘制简单的3D图形,就像在白纸上画立体图形一样. 主要内容: 视觉上的远近.3D空间.绘制一个空间图形 一.视觉上 ...
- UE5 GAS 学习笔记 1.2游戏标签
文章目录 前言 一.概念介绍 二.响应Gameplay Tags的变化 UE5 GAS 学习笔记目录 前言 这一节对GameplayTag游戏标签进行详细的讲解. 一.概念介绍 FGameplayTa ...
- unity学习笔记2-卡牌游戏的制作
unity学习笔记2 需求:制作卡牌游戏 第一步,建立card类,carddatabase类 card类删除自带的start和update方法,因为只是个对象类,负责装载对象即可 carddataba ...
- HTML5中canvas实现拼图游戏,HTML5 Canvas学习笔记(6)拼图游戏(数字版)
今天网上发现了一段代码,只有界面,很不错,学习了并完成了逻辑. 效果图: 点击这里试玩 http://www.108js.com/article/canvas/6/play.html 欢迎访问博主的网 ...
- Unity学习笔记—二次元日系游戏制作(理论篇)
原教程:siki:二次元日系游戏制作工具 - live2dSDK入门教程 http://www.sikiedu.com/my/course/282 一.准备工作 1.下载安装:Live2D_Cubis ...
- C++学习笔记——天天爱消除游戏实践
最近了一个天天爱消除的小游戏,以此来学习C++以及"graphic"头文件的应用,此程序参考了网上大神编写的程序源码. 一.代码编写 首先我们在VS2015中新建工程,首先编写头文 ...
最新文章
- iOS 命令行自动打包 (archive)
- 3D特效师可以下班了丨Science
- 80%的企业社会化商务应用可能无法取得预期效果
- IDEA Project Structure 配置说明
- HPU暑期第五次积分赛 F - 辞树的质因数分解
- go build -X 的妙用
- php smarty 序号,php – Smarty缩进和代码格式
- 身份验证和授权作为开源解决方案服务
- 使用FastReport报表工具生成图片格式文档
- 解决QQ未启用状态,QQ留言图标未启用
- python通过txt文件将指定图片复制到另一个文件夹
- iframe加载完成后操作contentDocument
- 华为认证hcia含金量_华为hcna认证含金量高吗?
- 在页面上动态显示实时时间
- Arduino传感器实验清单
- 手机下载的Termux如何利用you-get 下载视频
- C#开启和关闭UAC功能
- Apache网页与安全优化
- 利用qq账号查询所绑定的手机号码_怎么不用手机号注册新微信账号 免手机号申请方法分享...
- BZOJ1189 [HNOI2007]紧急疏散evacuate