Unity3D学习:射箭打靶游戏
这次做一个有点类似飞碟射击的游戏,射箭游戏
规则:靶有五环,射中不同的靶会相应加不同的分数(从红心到最外环分别加100,70,50,40,30,20),射出靶外会扣分200,中靶10次会有一次神箭效果(蓄满了能量),神箭中靶会相应加十倍分数而且会有特效,鼠标移动弓,而且每3秒变换一次风向,风向只有东风和西方,影响箭的飞向,所以要有预判能力。
先上效果图:
游戏架构跟飞碟的差不多,如下:
接下来讲讲靶和箭的制作:
靶的制作方法是用一个空物体装六个圆柱体,越内环越厚,半径越小,越外环越薄,半径越大(如图),这是组合模式,然后只在根物体上加刚体,C0到C5不加刚体但加网格碰撞器
就这样一个靶就形成了。
接下来是箭:
也是组合模式:根物体加刚体和网格碰撞器就行,子物体都不用加,body是圆柱体,箭头我用两个方块做
是不是看起来也挺疼的~~~
还有弓的模型我是在商店下载的,再直接拿来做预设
讲讲实现细节:弓跟着鼠标位置移动,只需要不断刷新弓的位置就行,射箭出去的实现跟射飞碟差不多,然后箭怎么停留在靶上呢?有一个技巧,可以用触发器把箭的刚体去掉,这样箭就会停留在靶上,至于神箭特效,就是简单加个爆炸粒子而已。看起来挺帅,是不是实现挺简单的?
接下来上代码:
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;}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Recorder : MonoBehaviour {private int _score = 0;void Start() {}public void AddScore(string _circle,bool full)//根据环的名字加相应分数,full判断是否神箭{Debug.Log(full);if (_circle == "C0") {if (full) _score += 1000;else _score += 100;}else if (_circle == "C1") {if (full) _score += 700;else _score += 70;}else if (_circle == "C2") {if (full) _score += 500;else _score += 50;}else if (_circle == "C3") {if (full) _score += 400;else _score += 40;}else if (_circle == "C4") {if (full) _score += 300;else _score += 30;}else if (_circle == "C5") {if (full) _score += 200;else _score += 20;}else if (_circle == "Plane") _score -= 200;}public int getScore() { return _score; }void Update () {}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class UserFace : MonoBehaviour {private SceneController _Controller;public Camera _camera;public Text Score;public Text WindForce;public Text WindDir;public Text energy;private float tempwind;private GameObject bow;public Vector3 bowpos;void Start () {_Controller = (SceneController)FindObjectOfType(typeof(SceneController));bow = Instantiate(Resources.Load("Prefabs/Bow")) as GameObject;}void Update () {bowpos = new Vector3(Input.mousePosition.x - 473, Input.mousePosition.y - 300, -7);//不断刷新弓的位置bow.transform.position = bowpos;tempwind = _Controller.getActionManager().getWingForce();//获取风力然后显示风向以及力度if (tempwind>0f){WindDir.text = "WindDirection:EAST";WindForce.text = "WindForce:" + tempwind;} else if(tempwind<0f){tempwind = -tempwind;WindDir.text = "WindDirection:WEST";WindForce.text = "WindForce:" + tempwind;} else{WindForce.text = "WindForce:" + tempwind;WindDir.text = "WindDirection:NoWind";}if (Input.GetMouseButtonDown(0)){Ray mouseRay = _camera.ScreenPointToRay(Input.mousePosition);_Controller.shootArrow(mouseRay.direction,bowpos); }Score.text = "Score:" + _Controller.getRecorder().getScore();energy.text = "Energy:" + _Controller.goalcount*10 + "%";}
}
箭工厂跟飞碟工厂差不多
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ArrowFactory : MonoBehaviour {public List<GameObject> Using; //储存使用中的箭矢 public List<GameObject> Used; //储存空闲箭矢 private GameObject arrowPrefab;void Start () {arrowPrefab = Instantiate(Resources.Load("Prefabs/arrow")) as GameObject;Using = new List<GameObject>();Used = new List<GameObject>();}public GameObject getArrow(){GameObject t;if (Used.Count == 0){t = GameObject.Instantiate(arrowPrefab);t.SetActive(true);}else{t = Used[0];t.SetActive(true);Used.Remove(t);if (t.GetComponent<Rigidbody>() == null)t.AddComponent<Rigidbody>();Component[] comp = t.GetComponentsInChildren<CapsuleCollider>();foreach (CapsuleCollider i in comp){i.enabled = true;}t.GetComponent<MeshCollider>().isTrigger = true;}Using.Add(t);return t;}private void freeArrow(){for (int i = 0; i < Using.Count; i++){GameObject t = Using[i];if (!t.activeInHierarchy){Using.RemoveAt(i);Used.Add(t);}}}void Update () {freeArrow();}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ArrowModule : MonoBehaviour {private string circle="";private float _time = 8f;private SceneController _Controller;public ParticleSystem _exposion;void OnTriggerEnter(Collider other){if(circle==""){circle = other.gameObject.name;Debug.Log(circle);if(circle!="Plane"){if(_Controller.goalcount==10)//是神箭{_Controller.goalcount = 0;_Controller.energyflag = true;_exposion.transform.position = other.transform.position;_exposion.Play();//播放神箭特效} else_Controller.goalcount++;}Destroy(GetComponent<Rigidbody>());Component[] comp = GetComponentsInChildren<CapsuleCollider>();//把箭的碰撞器取消以免与其他箭碰撞foreach (CapsuleCollider i in comp){i.enabled = false;}GetComponent<MeshCollider>().isTrigger = false;//防止箭再次触发_Controller.getRecorder().AddScore(circle, _Controller.energyflag);_Controller.energyflag = false;}}void Start () {_Controller = (SceneController)FindObjectOfType(typeof(SceneController));_exposion = GameObject.Instantiate(_exposion) as ParticleSystem;}void Update () {if (_time > 0) _time -= Time.deltaTime;else if(_time<=0){this.gameObject.SetActive(false);_time = 8f;circle = "";}}
}
SceneController以及动作管理器和飞碟的比较相似
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SceneController : MonoBehaviour {private ActionManager _Manager;private Recorder _Recorder;private ArrowFactory _Factory;public bool energyflag = false;public int goalcount=0;void Start(){Director _director = Director.getinstance();_director._Controller = this;_Manager = (ActionManager)FindObjectOfType(typeof(ActionManager));_Recorder = (Recorder)FindObjectOfType(typeof(Recorder));_Factory = (ArrowFactory)FindObjectOfType(typeof(ArrowFactory));_director.setFPS(60);_director._Controller = this;}public ArrowFactory getFactory(){return _Factory;}public Recorder getRecorder(){return _Recorder;}public ActionManager getActionManager(){return _Manager;}public void shootArrow(Vector3 dir,Vector3 bowpos){GameObject arrow = _Factory.getArrow(); arrow.transform.position = bowpos;_Manager.shoot(arrow, dir); }void Update () {}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ActionManager : MonoBehaviour {private SceneController _Controller;float _windforce = 0f;float _speed = 40f;private bool ischangewind = true;float _time = 3f;void Start () {_Controller = (SceneController)FindObjectOfType(typeof(SceneController));}public void shoot(GameObject _arrow,Vector3 _dir){_arrow.transform.up = _dir;_arrow.GetComponent<Rigidbody>().velocity = _dir * _speed;}public void WindSystem() //风力系统,每3秒变一次{for(int i=0;i< _Controller.getFactory().Using.Count;i++){if(_Controller.getFactory().Using[i].activeInHierarchy&& _Controller.getFactory().Using[i].GetComponent<Rigidbody>()){_Controller.getFactory().Using[i].GetComponent<Rigidbody>().AddForce(new Vector3(_windforce, 0, 0), ForceMode.Force);}}}public float getWingForce() { return _windforce; }private void setForce(){if(ischangewind){_windforce = Random.Range(-30f, 30f);ischangewind = false;}}void Update () {if (_time > 0) _time -= Time.deltaTime;else if(_time<=0){ischangewind = true;_time = 3f;}WindSystem();setForce();}
}
Unity3D学习:射箭打靶游戏相关推荐
- 【Unity3D】射箭打靶游戏(简单工厂+物理引擎编程)
打靶游戏: 1.靶对象为 5 环,按环计分: 2.箭对象,射中后要插在靶上: 3.游戏仅一轮,无限 trials: 增强要求: 添加一个风向和强度标志,提高难度 游戏成品图: ...
- Unity3D学习——射箭游戏(工厂模式)
成品展示 每两秒会获得一个稳定风速,玩家可以把握住这个瞬间射箭,击中不同环得分不同,不击中不扣分,60s内够100分则获胜 游戏制作 脚本挂载与预制 箭靶子预制 在一个空对象上挂载五个扁平同心圆柱,设 ...
- Unity3D——学习分享(一) 游戏开发
自学游戏开发也有一段时间了,很早就想把自己所学到的知识做个笔记总结一下,但因为种种的原因一直没能坚持下来,所以现在我打算现在开始把我学到的内容总结下来,主要的目的是: 把自己的所学所感记录下来,方便自 ...
- Unity3D学习:飞碟游戏进化版
上一个做的飞碟游戏虽然是功能也齐全,但是我感觉架构不是很好有点紊乱,不利于后期维护以及改进,所以我按照一个完整的架构重新做了一次,思路更清晰,而且还添加了更多的功能.这次的飞碟游戏是两个关卡,50分上 ...
- Unity3D学习之射箭小游戏
一.了解基础知识 对于射箭小游戏来说,新增加了物理引擎的运用.物理引擎主要包括三个方面:Rigidbody.Collide.PhysicMaterial.其中,Collider是最基本的触发物理的条件 ...
- 【Unity3d学习】使用物理引擎——打飞碟游戏的物理引擎改进与射箭游戏设计
文章目录 写在前面 HitUFO的物理引擎改进版本 物理引擎的改进版本思路与实现 PhysicsAction PhysicsManager 新接口类IActionManager 动作管理器基类的变化 ...
- Unity3D 学习笔记4 —— UGUI+uLua游戏框架
Unity3D 学习笔记4 -- UGUI+uLua游戏框架 使用到的资料下载地址以及基础知识 框架讲解 拓展热更过程 在这里我们使用的是uLua/cstolua技术空间所以提供的UGUI+uLua的 ...
- Unity3D学习之打飞碟游戏
首先,先上设计图: 效果图: 预设设置: 简单说一下设计思路.对于飞碟的运动:运动学运动,有点类似牧师恶魔里面的过河:物理运动,则类似射箭小游戏的设计.所以,对于这两种动作分别实现即可. 运动学实现: ...
- ch06-物理系统与碰撞——Arrowshooting射箭小游戏
游戏内容要求: 1.靶对象为 5 环,按环计分: 2.箭对象,射中后要插在靶上 增强要求:射中后,箭对象产生颤抖效果,到下一次射击 或 1秒以后 3.游戏仅一轮,无限 trials: 增强要求:添加一 ...
最新文章
- OpenCV中的姿势估计及3D效果(3D坐标轴,3D立方体)绘制
- WinExec、ShellExecute用法详解
- spring boot中@ResponseBody等注解的作用与区别
- sub在python中的意义_在python中,如何使用回复sub?
- python符号运算_用Python做科学计算-SymPy符号运算
- LiveVideoStack线上交流分享 ( 五 ) —— 在线教育音视频技术探索与应用
- Linux网络那点事
- day22 java的枚举
- poj 2777 AND hdu 5316 线段树
- 求100以内的所有素数
- Sibelius for Mac 8.2.0 谱曲软件 中文破解版下载
- word文档字间距怎么调?拯救死气沉沉的文字仅需这样…
- 启智树游记题解——逆境中的奇迹
- J. 青出于蓝胜于蓝(dfs序+树状数组)
- 基于darknet的voc数据集训练和mAP测试
- Maximo 人员- 应用程序导入,公共操作怎么配置,求大神指点,万分感谢
- 常用期刊、会议的简称缩写(深度学习图像处理领域)
- java自动转换与强制转换
- RRDtool简体中文教程
- android 调用系统相机拍照并返回路径,Android调用相机拍照并返回路径和…
热门文章
- .DS_Store 文件是什么? / .DS_Store 文件是什么macOS
- sublime工具的使用
- JAVA自动生成雪碧图sprites和样式CSS文件(包含原始图标CSS、雪碧图CSS)
- c语言解析ip的主要代码
- 大气复折射率matlab,MATLAB计算微纳光纤有效折射率和论文不符
- mattermost之数据库操作
- HTTP1.1协议中文版
- 移动端/PC端网页开发建议
- SQL52 获取employees中的first_name
- zabbix监控项配置—带宽/磁盘/CPU/内存/IIS/事件日志