Unity5.x製作RPG遊戲
阿新 • • 發佈:2019-02-04
先展示下執行效果圖:
這裡主要介紹主角的控制,敵人的AI邏輯,相機的跟隨
可以參考遊戲原始碼來分析點選開啟連結
一.主角的控制
新增尋路元件,以及Animation動畫元件
CustomEvent.cs
using UnityEngine; using System.Collections; public class customEvent : MonoBehaviour { /// <summary> /// 尋路元件 /// </summary> private NavMeshAgent nav; /// <summary> /// 動作元件 /// </summary> private Animation ani; // Use this for initialization void Start () { //獲取尋路元件 nav=this.transform.GetComponent<NavMeshAgent>(); //獲取動作元件 ani=this.transform.GetComponent<Animation>(); } // Update is called once per frame void Update () { //按鍵 if(Input.GetKey(KeyCode.Z)) { nav.SetDestination(this.transform.position); ani.CrossFade("swordStrike1"); } if(Input.GetKey(KeyCode.X)) { nav.SetDestination(this.transform.position); ani.CrossFade("swordStrike2"); } if(Input.GetKey(KeyCode.C)) { nav.SetDestination(this.transform.position); ani.CrossFade("swordStrike3"); } //按下滑鼠 if (Input.GetMouseButtonDown (0)) { //玩家移動 MoveCharactor (Input.mousePosition); } if(Vector3.Distance(this.transform.position,hit.point)<=0.1f) { //停止動作,恢復原來預設動作 ReturnIdle(); } } private RaycastHit hit; void MoveCharactor(Vector3 mousePos) { Ray ray = Camera.main.ScreenPointToRay (mousePos); //將變數hit回撥給函式,進行相應處理 if(Physics.Raycast(ray,out hit)) { if(hit.collider.CompareTag("terrain")) { //切換動作 ani.CrossFade("runSword"); //開始尋路 nav.SetDestination(hit.point); nav.speed=5.0f; } if(hit.collider.CompareTag("Enemmy")) { Vector3 enemyPos = hit.transform.position; this.transform.rotation=Quaternion.LookRotation(enemyPos); nav.SetDestination(hit.point); nav.speed=6.0f; } } } void ReturnIdle() { ani.CrossFade ("swordStrike3"); } }
二.敵人的人工智慧邏輯
敵人的AI邏輯與我之前上一篇文章類似參考
EnemyAI.cs
using UnityEngine; using System.Collections; public class EnemyAI : MonoBehaviour { /// <summary> /// 尋路元件 /// </summary> private NavMeshAgent nav; /// <summary> /// 動作元件 /// </summary> private Animation ani; /// <summary> /// 主角 /// </summary> private Transform player; /// <summary> /// 主角與敵人的距離 /// </summary> public float distance=15; /// <summary> ///是否尋路狀態 /// </summary> private bool isFund=false; /// <summary> /// 隨機值 /// </summary> private int RANDrange=20; /// <summary> /// 隨機位置 /// </summary> Vector3 RandVector; // Use this for initialization void Start () { //獲取尋路元件 nav=this.transform.GetComponent<NavMeshAgent>(); //獲取動作元件 ani = this.transform.GetComponent<Animation> (); //獲取主角 player = GameObject.FindGameObjectWithTag ("Player").transform; } // Update is called once per frame void Update () { float m_distance = Vector3.Distance (this.transform.position, player.position); //敵人在視察範圍內 if (m_distance <= distance) { isFund=false; //面向主角 //this.transform.rotation=Quaternion.LookRotation(player.position); if (m_distance <= player.GetComponent<NavMeshAgent> ().radius + nav.radius+4.0f) { ani.CrossFade ("walk"); } else { ani.CrossFade ("run"); //重新尋路 nav.Resume(); nav.SetDestination (player.position); nav.speed = 3.0f; } } //不在視察範圍內,敵人自動行走 else { if(!isFund) { isFund=true; RandVector=new Vector3(Random.Range(-RANDrange,RANDrange),0,Random.Range(-RANDrange,RANDrange))+this.transform.position; ani.CrossFade("walk"); nav.SetDestination(RandVector); } if(Vector3.Distance(this.transform.position,RandVector)<=0.02f) { isFund=!isFund; } } } }
三.相機的跟隨
(1)按下A和D鍵相機將繞著主角旋轉
(2)滾動滑鼠滑輪,將擴大和縮放場景
using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour { /// <summary> /// 跟隨目標 /// </summary> public Transform target; /// <summary> /// 平滑速度 /// </summary> public float speed=3.6f; /// <summary> /// 攝像機與主角方位距離 /// </summary> private Vector3 offset; // Use this for initialization void Start () { offset = this.transform.position - target.position; } // Update is called once per frame void Update () { if (Input.GetKey (KeyCode.A)) { this.transform.RotateAround (target.transform.position, Vector3.up, Time.deltaTime * speed*2); } if (Input.GetKey (KeyCode.D)) { this.transform.RotateAround (target.transform.position, Vector3.up, -Time.deltaTime * speed*2); } //平滑移動攝像機 SmoothMoveCamera (); //滾動輪子 if (Input.GetAxis ("Mouse ScrollWheel") < 0) { if(Camera.main.fieldOfView<=80.0f) { Camera.main.fieldOfView+=5.0f; } } if (Input.GetAxis ("Mouse ScrollWheel") > 0) { if(Camera.main.fieldOfView>=20.0f) { Camera.main.fieldOfView-=5.0f; } } } void SmoothMoveCamera() { Vector3 cameraFrom = this.transform.position; Vector3 cameraTo = target.position + offset; this.transform.position =Vector3.Lerp (cameraFrom, cameraTo, Time.deltaTime*speed); } }
本人也在尋找一份遊戲開發實習工作,如果大佬們需要開發人員,請把我帶走
作品的話可以私聊我哦!