首先是玩家的动画控制器: 巡逻兵的动画控制器:
patrol的设置: player的设置: Plane的设置: Plane的子对象也做了不少设置,这里不再详细给出。
Trigger的设置:
在预制部分,我们看到Trigger和patrol里面都挂载了ColiAction这个脚本,来实现玩家与巡逻兵碰撞时的通知:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ColiAction : MonoBehaviour { FirstSceneController sceneController; GameObject myobject = null; GameObject player; public delegate void AddScore(); public static event AddScore myAddScore; public delegate void GameOver(); public static event GameOver myGameOver; private void Start() { sceneController = SSDirector.getInstance().currentSceneController as FirstSceneController; player = sceneController.player; } private void OnTriggerEnter(Collider other) { if (other.gameObject == player) { int k = this.name[this.name.Length - 1] - '0'; myobject = sceneController.patrols[k]; foreach (var i in sceneController.actionManager.seq) { if (i.gameObject == myobject) { i.enable = false; Vector3 a = new Vector3(myobject.transform.position.x, 0, myobject.transform.position.z); Vector3 b = new Vector3(player.transform.position.x, 0, player.transform.position.z); Quaternion rotation = Quaternion.LookRotation(b - a, Vector3.up); myobject.transform.rotation = rotation; } } } } private void OnTriggerExit(Collider other) { if (other.gameObject == player) { int k = this.name[this.name.Length - 1] - '0'; foreach (var i in sceneController.actionManager.seq) { if (i.gameObject == myobject) { i.enable = true; Vector3 a = new Vector3(myobject.transform.position.x, 0, myobject.transform.position.z); Vector3 b = new Vector3(i.target.x, 0, i.target.z); Quaternion rotation = Quaternion.LookRotation(b - a, Vector3.up); myobject.transform.rotation = rotation; } } myobject = null; myAddScore(); } } private void OnCollisionEnter(Collision collision) { if (this.tag == "patrol" && collision.gameObject == player) { myGameOver(); var k = collision.gameObject.GetComponent<Animator>(); k.SetBool("death", true); } } private void Update() { if (myobject != null && sceneController.flag == 0) { myobject.transform.position = Vector3.MoveTowards(myobject.transform.position, player.transform.position, 3 * Time.deltaTime); } } }在一个空对象挂载FirstSceneController脚本,来加载场景:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class FirstSceneController : MonoBehaviour, IUserAction, ISceneController { public CCActionManager actionManager; public GameObject player; public List<GameObject> patrols = new List<GameObject>(); public PatrolFactory pf; public int flag = 0; public int score = 0; private void Awake() { SSDirector director = SSDirector.getInstance(); director.setFPS(60); director.currentSceneController = this; this.gameObject.AddComponent<PatrolFactory>(); pf = Singleton<PatrolFactory>.Instance; this.gameObject.AddComponent<UserGUI>(); director.currentSceneController.GenGameObjects(); this.gameObject.AddComponent<CCActionManager>(); } private void OnEnable() { ColiAction.myAddScore += AddScore; ColiAction.myGameOver += GameOver; } private void OnDisable() { ColiAction.myAddScore -= AddScore; ColiAction.myGameOver -= GameOver; } private void GameOver() { Pause(); flag = 1; } private void Start() { } public void GenGameObjects() { int count = 0; GameObject plane = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Plane")); plane.transform.parent = this.transform; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (i == 0 && j == 2) { player = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/player")); player.transform.position = new Vector3(plane.transform.position.x + 9 * (i - 1), 0, plane.transform.position.z + 9 * (j - 1)); if (player.GetComponent<Rigidbody>()) { player.GetComponent<Rigidbody>().freezeRotation = true; } } else { GameObject trigger = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Trigger")); trigger.transform.parent = plane.transform; trigger.transform.position = new Vector3(plane.transform.position.x + 9 * (i - 1), 0, plane.transform.position.z + 10 * (j - 1)); trigger.name = "trigger" + count; count++; GameObject patrol = pf.GetPatrol(); patrol.transform.position = trigger.transform.position; patrols.Add(patrol); } } } } public void Restart() { SceneManager.LoadScene("1"); } public void Pause() { actionManager.Pause(); } private void AddScore() { score++; } }CCActionManager脚本用来管理动作以及用户输入:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CCActionManager : SSActionManager, ISSActionCallback { public FirstSceneController sceneController; public List<CCMoveToAction> seq = new List<CCMoveToAction>(); public UserAction userAction; public PatrolFactory pf; protected new void Start() { sceneController = (FirstSceneController)SSDirector.getInstance().currentSceneController; userAction = UserAction.GetSSAction(5); sceneController.actionManager = this; pf = sceneController.pf; RunAction(sceneController.player, userAction, this); foreach (var i in sceneController.patrols) { float x = Random.Range(-3.0f, 3.0f); int z = Random.Range(0, 4); Vector3 target = new Vector3(z % 2 == 0 ? (z - 1) * 3 : x, 0, z % 2 != 0 ? (z - 2) * 3 : x); CCMoveToAction k = CCMoveToAction.GetSSAction(target + i.transform.position, 100, i.transform.position); seq.Add(k); Quaternion rotation = Quaternion.LookRotation(target, Vector3.up); i.transform.rotation = rotation; RunAction(i, k, this); } } protected new void Update() { base.Update(); } public void SSActionEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null) { if (source != userAction) { CCMoveToAction cCMoveTo = source as CCMoveToAction; float x = Random.Range(-3.0f, 3.0f); int z = Random.Range(0, 4); Vector3 target = new Vector3(z % 2 == 0 ? (z - 1) * 3.0f : x, 0, z % 2 == 0 ? x : (z - 2) * 3.0f); CCMoveToAction k = CCMoveToAction.GetSSAction(target + cCMoveTo.initPosition, 1.5f, cCMoveTo.initPosition); seq.Remove(cCMoveTo); source.destory = true; seq.Add(k); Quaternion rotation = Quaternion.LookRotation(target + cCMoveTo.initPosition - source.transform.position, Vector3.up); source.transform.rotation = rotation; RunAction(source.gameObject, k, this); } } public void CheckEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null) { } public void Pause() { if (sceneController.flag == 0) { foreach (var k in seq) { k.enable = false; k.gameObject.GetComponent<Animator>().SetBool("running", false); } userAction.enable = false; sceneController.flag = 2; } else if (sceneController.flag == 2) { foreach (var k in seq) { k.enable = true; k.gameObject.GetComponent<Animator>().SetBool("running", true); } userAction.enable = true; sceneController.flag = 0; } } }UserAction脚本用来处理player的移动:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class UserAction : SSAction { // Use this for initialization private float speed; private float rspeed = 90; public static UserAction GetSSAction(float speed) { UserAction action = CreateInstance<UserAction>(); action.speed = speed; return action; } public override void Start() { } public override void Update() { if (enable) { float translationX = Input.GetAxis("Horizontal"); float translationZ = Input.GetAxis("Vertical"); if (translationX != 0 || translationZ != 0) { gameObject.GetComponent<Animator>().SetBool("running", true); } else { gameObject.GetComponent<Animator>().SetBool("running", false); } gameObject.transform.Translate(translationX * speed * Time.deltaTime * 0.1f, 0, translationZ * speed * Time.deltaTime); gameObject.transform.Rotate(0, translationX * rspeed * Time.deltaTime, 0); if (gameObject.transform.localEulerAngles.x != 0 || gameObject.transform.localEulerAngles.z != 0) { gameObject.transform.localEulerAngles = new Vector3(0, gameObject.transform.localEulerAngles.y, 0); } if (gameObject.transform.position.y != 0) { gameObject.transform.position = new Vector3(gameObject.transform.position.x, 0, gameObject.transform.position.z); } } } }