然后是用rigidbody的处理来实现一个射箭游戏

这里贴上游戏效果和代码

ArrowController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class ArrowController {GameObject arrow;Vector3 startPos;ArrowScript arrowScript;Vector3 resetForce;public ArrowController(GameObject obj) {arrow = obj;startPos = new Vector3 (0, 0, 0);arrow.transform.position = startPos;arrowScript = obj.AddComponent<ArrowScript> ();obj.AddComponent<MoveAction> ();arrowScript.arrow = this;resetForce = new Vector3 (0, 0, -10);}public GameObject getGameObj() {return arrow;}public void appear() {this.arrow.SetActive (true);}public void disappear() {this.arrow.SetActive (false);}public void setPosition(Vector3 pos) {this.arrow.transform.position = pos;}public Rigidbody getRigidBody() {return arrow.GetComponent<Rigidbody> ();}public void reset() {Debug.Log ("reset...");
//          this.arrow.AddComponent<Rigidbody> ();
//          this.arrow.transform.position = startPos;this.getRigidBody ().AddForce (resetForce);}}
}

ArrowFactory.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class ArrowFactory : MonoBehaviour {Queue<ArrowController> freeQueue;List<ArrowController> usingList;GameObject originalArrow;Vector3 startPos;void Awake() {freeQueue = new Queue<ArrowController> ();usingList = new List<ArrowController> ();originalArrow = Object.Instantiate (Resources.Load ("prefabs/arrow", typeof(GameObject))) as GameObject;
//          originalArrow.AddComponent<BoxCollider> ();originalArrow.AddComponent<OnCollision> ();originalArrow.AddComponent<Rigidbody> ();originalArrow.GetComponent<Rigidbody> ().collisionDetectionMode = CollisionDetectionMode.Continuous;originalArrow.GetComponent<Rigidbody> ().useGravity = false;
//          originalArrow.GetComponent<Collider> ().isTrigger = true;originalArrow.SetActive (false);startPos = new Vector3 (0, 0, 0);}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}public ArrowController produceArrow() {Debug.Log ("producing arrow...");ArrowController newArrow;newArrow = null;if (freeQueue.Count == 0) {GameObject newObj = GameObject.Instantiate (originalArrow);
//              newObj.AddComponent<MoveAction> ();newArrow = new ArrowController (newObj);} else {newArrow = freeQueue.Dequeue ();}newArrow.appear ();
//          newArrow.setPosition (startPos);usingList.Add (newArrow);return newArrow;}public void recycle (ArrowController arrow) {Debug.Log ("recycle in ArrowFactory...");arrow.disappear ();arrow.reset ();arrow.getGameObj ().transform.position = startPos;usingList.Remove (arrow);freeQueue.Enqueue (arrow);}}
}

ArrowScript.cs(也是为了能从GameObject获取到Controller)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class ArrowScript : MonoBehaviour {public ArrowController arrow;// Use this for initializationvoid Start () {for (int i = 0; i < transform.childCount; i++){GameObject g = transform.GetChild(i).gameObject;g.AddComponent<ArrowScript>().arrow = arrow;}}// Update is called once per framevoid Update () {}}
}

Director.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class Director : System.Object {private static Director director;public SceneController currentSceneController;public static Director getInsance() {if (director == null) {director = new Director ();}return director;}}
}

FirstSceneController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using game;namespace game {public class FirstSceneController : MonoBehaviour, SceneController, userAction {Director director;ArrowController arrow;ArrowFactory af;MoveAction ma;GameObject target;GameObject ground;GameObject wall;Scorer scorer;UserGUI UI;GameObject canvas;Text scoreText;OnCollision oc;
//      TargetController target;// Use this for initializationvoid Awake() {Director director = Director.getInsance ();director.currentSceneController = this;}public void loadResources() {target = GameObject.Instantiate (Resources.Load("prefabs/targetBasis")) as GameObject;
//          target.AddComponent<Rigidbody> ();
//          target.AddComponent<OnCollision> ();
//          target.GetComponentInChildren<Collider> ().isTrigger = true;target.transform.position = new Vector3 (0, 0.3f, 0);ground = GameObject.Instantiate (Resources.Load("prefabs/ground")) as GameObject;wall = GameObject.Instantiate (Resources.Load("prefabs/wall")) as GameObject;}void Start () {loadResources ();af = GetComponent<ArrowFactory> ();ma = GetComponent<MoveAction> ();scorer = GetComponent<Scorer> ();UI = GetComponent<UserGUI> ();oc = GetComponent<OnCollision> ();
//          oc.target = target;canvas = GameObject.Instantiate (Resources.Load("prefabs/Canvas")) as GameObject;scoreText = canvas.transform.Find ("Score").GetComponent<Text> ();scoreText.text = "Score: 0";}// Update is called once per framevoid Update () {scoreText.text = "Score: " + scorer.getScore ();}public void startGame() {Debug.Log ("Game starts...");nextShoot ();}public void OnCollision(GameObject obj) {
//          Debug.Log (obj.GetComponent<MeshRenderer>().material.color);Debug.Log ("The obj name is " + obj.name);scorer.addScore (obj);}public void nextShoot() {arrow = af.produceArrow ();ma = arrow.getGameObj ().GetComponent<MoveAction> ();//testDebug.Log("arrow is " + arrow.ToString());
//          Debug.Log ("judgement: "+ (arrow==null).ToString());ma.setArrow (arrow);Debug.Log ("ma's Arrow is "+arrow.ToString());ma.setShot (false);oc.setFlag (false);
//          nextShoot ();}public void recycle (ArrowController arrow) {
//          ma = arrow.getGameObj ().GetComponent<MoveAction> ();//testDebug.Log ("recycling..." + arrow);if (arrow != null) {af.recycle (arrow);}}}
}

interface.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public interface userAction {void startGame();}public interface SceneController {void loadResources();}
}

MoveAction.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class MoveAction : MonoBehaviour {public ArrowController arrow;public GameObject arr;Vector3 force;bool shot = false;Camera cam;// Use this for initializationvoid Start () {force = new Vector3 (0, 0, 10);shot = false;arrow = null;arr = this.gameObject;cam = Camera.main;}// Update is called once per framevoid Update () {
//          this.transform.Find ("head").LookAt (Input.mousePosition);
//          print(this);
//          this.transform.LookAt (Input.mousePosition);Ray ray = cam.ScreenPointToRay(Input.mousePosition);
//          print (this.name);
//          this.transform.rotation = Quaternion.FromToRotation(Vector3.down, ray.direction);
//          Debug.Log(shot);Debug.Log ("test of " + shot.ToString());if (!shot && this.name == "arrow(Clone)(Clone)")  {
//              Debug.Log ("set rotation...");this.transform.rotation = Quaternion.FromToRotation (Vector3.forward, ray.direction);}
//          Debug.DrawRay (cam.transform.position, Input.mousePosition,Color.blue);
//          arrow.transform.rotation = Quaternion.FromToRotation(Vector3.up * -1, ray.direction);if (Input.GetMouseButtonDown(0)) {
//              Debug.Log ("Button down...");shot = true;}}public void setShot(bool flag) {shot = flag;}public void setArrow(ArrowController arr) {Debug.Log ("setting new arrow...");arrow = arr;}void FixedUpdate() {
//          Debug.Log ("current arrow: " + this.arrow.ToString());
//          Debug.Log ("judgement: "+ (arrow==null).ToString());
//          if (arrow == null)
//              return;if (arr == null)return;
//          Rigidbody rigid = arrow.getRigidBody();Rigidbody rigid = arr.GetComponent<Rigidbody> ();Debug.Log ("RigidBody is " + rigid.ToString());if (rigid && shot) {
//              Debug.Log (shot);Debug.Log ("Adding the force");rigid.AddForce (force);}}}
}

OnCollision.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class OnCollision : MonoBehaviour {FirstSceneController sceneController;GameObject obj;bool flag; // 判断是否碰到第一个
//      public GameObject target;// Use this for initializationvoid Start () {Debug.Log ("OnCollision started...");sceneController = Director.getInsance ().currentSceneController as FirstSceneController;flag = false;}// Update is called once per framevoid Update () {}public void setFlag(bool f) {flag = f;}void OnCollisionEnter(Collision collision) {if (flag)return;
//          Debug.Log (collision.collider.gameObject.name);if (collision.collider.gameObject.name == "wall(Clone)") {sceneController.recycle (this.gameObject.GetComponent<ArrowScript> ().arrow);Debug.Log ("next...");sceneController.nextShoot ();return;
//              sceneController.recycle (this);}Debug.Log ("Collision...");Destroy (this.GetComponent<Rigidbody> ());obj = collision.collider.gameObject;
//          GameObject target = collision.collider.transform.parent.gameObject;
//          Component[] comp = target.GetComponentsInChildren<Collider> ();
//          Debug.Log (comp.Length);
//          foreach (Collider i in comp) {Debug.Log ("Disabed one round...");
//              if (i.gameObject.name.StartsWith("target")) i.enabled = false;
//          }sceneController.OnCollision (obj);flag = true;if (flag) {Debug.Log ("next...");sceneController.nextShoot ();}
//          if (collision.collider.name == "target")
//              parent = collision.collider.transform.parent;
//          if (collision.collider.name == "arrow(Clone)(Clone)") {
//              Debug.Log ("Destroy Rigidbody...");
//              Destroy (collision.collider.gameObject.GetComponent<Rigidbody> ());
//          }
//          Debug.Log ("Enter the OnCollisionEnter...");
//          Debug.Log ("GameObject is : " + collision.gameObject.name);
//          if (collision.collider) {
//              Debug.Log ("Collider belongs to : " + collision.collider.gameObject.name);
//          }
//          if (collision.rigidbody) {
//              Debug.Log ("Rigidbody belongs to : " + collision.rigidbody.gameObject.name);
//          }
//          foreach(ContactPoint contact in collision.contacts) {
//              Debug.Log ("show rays...");
//              Debug.DrawRay (contact.point, contact.normal, Color.blue);
//          }}//      void OnTriggerEnter(Collider collider) {
//          Debug.Log ("Collision...");
//          //          if (collision.collider.name == "target")
//          //              parent = collision.collider.transform.parent;
//          //          if (collision.collider.name == "arrow(Clone)(Clone)") {
//          //              Debug.Log ("Destroy Rigidbody...");
//          //              Destroy (collision.collider.gameObject.GetComponent<Rigidbody> ());
//          //          }
//          //          Debug.Log ("Enter the OnCollisionEnter...");
//          Debug.Log ("GameObject is : " + collider.gameObject.name);if (collider) {Debug.Log ("Collider belongs to : " + collider.gameObject.name);}
//          //          foreach(ContactPoint contact in collision.contacts) {
//          //              Debug.Log ("show rays...");
//          //              Debug.DrawRay (contact.point, contact.normal, Color.blue);
//          //          }
//      }}
}

Scorer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class Scorer : MonoBehaviour {int score;// Use this for initializationvoid Start () {score = 0;}// Update is called once per framevoid Update () {}public int getScore() {return score;}public void addScore(GameObject obj) {if (obj.name.StartsWith("target0")) {score += 1;} else if (obj.name.StartsWith("target1")) {score += 2;} else if (obj.name.StartsWith("target2")) {score += 3;} else if (obj.name.StartsWith("target3")) {score += 4;} else if (obj.name.StartsWith("target4")) {score += 5;}}}
}

UserGUI.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using game;namespace game {public class UserGUI : MonoBehaviour {userAction action;bool started;// Use this for initializationvoid Start () {action = Director.getInsance ().currentSceneController as userAction;started = false;}// Update is called once per framevoid Update () {}void OnGUI() {if (!started) {if (GUI.Button(new Rect (Screen.width / 2 - 70, Screen.height / 2, 140, 70), "start")) {action.startGame ();started = true;}}}}
}

Unity3D学习(四)射箭游戏相关推荐

  1. Unity3D学习:射箭打靶游戏

    这次做一个有点类似飞碟射击的游戏,射箭游戏 规则:靶有五环,射中不同的靶会相应加不同的分数(从红心到最外环分别加100,70,50,40,30,20),射出靶外会扣分200,中靶10次会有一次神箭效果 ...

  2. Unity3D学习之射箭小游戏

    一.了解基础知识 对于射箭小游戏来说,新增加了物理引擎的运用.物理引擎主要包括三个方面:Rigidbody.Collide.PhysicMaterial.其中,Collider是最基本的触发物理的条件 ...

  3. Unity3D——学习分享(一) 游戏开发

    自学游戏开发也有一段时间了,很早就想把自己所学到的知识做个笔记总结一下,但因为种种的原因一直没能坚持下来,所以现在我打算现在开始把我学到的内容总结下来,主要的目的是: 把自己的所学所感记录下来,方便自 ...

  4. Unity3D学习:飞碟游戏进化版

    上一个做的飞碟游戏虽然是功能也齐全,但是我感觉架构不是很好有点紊乱,不利于后期维护以及改进,所以我按照一个完整的架构重新做了一次,思路更清晰,而且还添加了更多的功能.这次的飞碟游戏是两个关卡,50分上 ...

  5. 【Unity3d学习】使用物理引擎——打飞碟游戏的物理引擎改进与射箭游戏设计

    文章目录 写在前面 HitUFO的物理引擎改进版本 物理引擎的改进版本思路与实现 PhysicsAction PhysicsManager 新接口类IActionManager 动作管理器基类的变化 ...

  6. Unity3D学习(8)之射箭游戏

    射箭游戏: 靶对象为5环,按环计分,最内环为5分,最外环为1分,出靶则-2分: 箭对象,射中后要插在靶上: 游戏无限轮,每轮10trials达到要求分数即可进入下一轮: 增强要求:     添 ...

  7. unity3d 学习笔记四 skybox(天空盒) light(光源) halo(光晕)

    Unity3D学习笔记(四)天空.光晕和迷雾 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的地方,深深印在我的脑海里.当时在艾泽拉斯大陆还不能使用飞行坐骑,试 ...

  8. Unity3D 从入门到放弃(五)----射箭游戏

    Unity3D 从入门到放弃(五) --射箭游戏 填坑啊填坑,每周都补上周作业,啥时候才能到头啊= = 作业需求 游戏规则: 设计一个射箭小游戏,点击鼠标射箭:  靶对象为 5 环,按环计分:  ...

  9. Unity3D 学习笔记4 —— UGUI+uLua游戏框架

    Unity3D 学习笔记4 -- UGUI+uLua游戏框架 使用到的资料下载地址以及基础知识 框架讲解 拓展热更过程 在这里我们使用的是uLua/cstolua技术空间所以提供的UGUI+uLua的 ...

最新文章

  1. NumPy基础用法总结
  2. 1.2.4 ORACLE_SID的含义
  3. MySQL—02—MySQL的操作
  4. 基于语言模型的少样本学习 / 深度学习优化器基准测试 | 本周值得读
  5. [Leetcode总结] 102.二叉树的层序遍历
  6. 不礼让行人怎么抓拍的_【交通违法曝光2019年第11期】梅河口市公安局交警大队对电子警察抓拍的88台机动车不礼让行人车辆曝光如下...
  7. python把回车作为输入_python将回车作为输入内容的实例
  8. telnet服务下载 Linux,linux telnet服务安装包
  9. 长沙医学院学位计算机考试内容,湖南长沙医学院2017年9月计算机等级考试报名时间...
  10. Java笔记-通过注解和插件自动生成get/set和toString方法,使代码结构清晰
  11. 9 WM层面 临时仓储类型的仓位 主数据不存在
  12. 盼达用车:由于公司经营原因 2月1日起暂停运营
  13. iOS AVPlayer支持播放的格式
  14. 关于金山词霸2009牛津版在windows2003下无法对PDF文件取词的问题
  15. 产品经理需要看懂接口文档么?
  16. 计算机ps2定义,PS2通信协议说明及接口定义(键盘及鼠标).doc
  17. iPhone, iPad, 的Safari书签和阅读列表不同步问题
  18. 机械设计基础B【4】齿轮安装传动
  19. 微信小程序-H5-uniapp css制作上下跳动的柱状图——频谱
  20. 牛客网-Verilog篇

热门文章

  1. DB2、MySQL去重SQL
  2. Layui时间选择设置只能选择整点半点,并隐藏秒钟部分
  3. 年轻人为何如此反感团建?
  4. Windows Server 2012 R2 分布式文件系统(DFS)-深博-专题视频课程
  5. (二)以太坊——在私有链进行转账操作
  6. C#导出CSV后首位数字0没了,大神们进来吧
  7. ​包装“华丽且真实”的外汇资金盘,真的就无法避免吗?
  8. PDA汽车检测系统收获
  9. UE4Android聚光灯投影,ue4商城资源Volumetric Spotlight with Shadow带阴影的体积聚光灯
  10. 性能测试诊断(CPU, 内存)