1. 程式人生 > >unity3d-程式碼控制遊戲角色控制器移動

unity3d-程式碼控制遊戲角色控制器移動

先上一個gif看看效果。因為圖片大小限制。所以錄製的比較小。個人認為效果比較牽強。特別是裡面的邏輯程式碼。 

工程結構圖

 這次實現的效果是:

 1:攝像機跟著角色移動,上篇部落格說的是。把攝像機變成角色的子物件。發現沒有我想要的效果。跟著角色移動程式碼是我自己弄出來的。不知道實際專案中是否也是這樣。

 2:利用角色控制器組建 CharacterController 實現角色移動。

 3:當鼠左鍵擊地形,角色自動尋路。今天發現用unity3d有自帶的NavMesh實現自帶尋路,不過還沒去研究。

 4:當鼠左鍵點選地形。實現一個高亮效果。

 5:滑鼠右鍵單擊播放殺怪動畫。

 6:到達關卡場景,進入下一關。本來是想殺boss後在進入,但血條不會弄。

 ..............

Hierarchy檢視中元素。分別是:主攝像機,平行光,地形,牆(也就是我這裡的關卡),無效的元素,當前角色,滑鼠點選顯示圖片,boss

載入場景的時候。需要把載入的場景Build進去

 

最後看看實現程式碼 ,有點牽強。也寫了很多註釋。方便大家和自己以後檢視。

複製程式碼
  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 public class chartMove : MonoBehaviour
5 { 6 7 public float speed = 5f; 8 public GameObject cursor;//滑鼠點選游標顯示圖片 9 public GameObject boos; //boos物件 10 11 12 private Vector3 targetPos; //目標位置 13 private Vector3 mousePos; //滑鼠單擊的位置 14 private Vector3 beforePos;//角色移動前的位置 15 private CharacterController charController = null
; 16 private bool cursorState = false; //當前是否點選了滑鼠 17 private bool BossIsDie;//bos是否over 18 19 // Use this for initialization 20 void Start() 21 { 22 charController = GetComponent<CharacterController>(); 23 targetPos = transform.position; 24 mousePos = transform.position; 25 } 26 27 // Update is called once per frame 28 void Update() 29 { 30 beforePos = transform.position; //儲存當前的位置 31 32 //獲取按鍵軸 33 /* 34 * Horizontal-->對應鍵盤上上的A,D 35 * Vertical-->對應鍵盤上上的W,S 36 * 當沒有按鍵的時候為0 。 37 * 其值分別為 -1(左,下) 0 1(上。右) 38 */ 39 float x = Input.GetAxis("Horizontal"); 40 float y = Input.GetAxis("Vertical"); 41 42 //transform.Translate(new Vector3(x, 0, y) * Time.deltaTime * speed,Space.World); 43 44 //if (x == 0 && y == 0 && !cursorState) 45 //{ 46 // transform.animation.Play("idle"); 47 //} 48 if (x != 0 || y != 0) 49 { 50 transform.animation.Play("run"); //奔跑 51 cursor.SetActive(false); 52 //charController.Move(new Vector3(x, 0, y) * Time.deltaTime * speed); 53 //transform.Translate(new Vector3(x, 0, y) * Time.deltaTime * speed, Space.World); 54 cursorState = false; 55 charController.SimpleMove(new Vector3(x, 0, y) * speed); 56 57 //設定角色面朝的方向 58 Vector3 lookPos = transform.position - beforePos + transform.position; 59 transform.LookAt(lookPos); 60 } 61 else 62 { 63 64 } 65 66 67 //左鍵選擇路徑 68 if (Input.GetMouseButtonDown(0)) 69 { 70 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 71 RaycastHit hit; 72 if (Physics.Raycast(ray, out hit, 100)) //100為射線的長度,限制使用者瞎點選。在100以為點選無效 73 { 74 if (hit.collider.name == "Terrain") 75 { 76 mousePos = hit.point; 77 Vector3 lookPos = mousePos - transform.position + transform.position; 78 transform.LookAt(lookPos); 79 cursor.SetActive(true); //顯示圖片:圖片一開始是隱藏的。 80 cursor.transform.position = mousePos; 81 cursorState = true; 82 } 83 } 84 } 85 86 87 //判斷角色是否到達目的地 88 int distance = (int)Vector3.Distance(mousePos, transform.position); 89 90 //這個1.08的臨界值。我是根據除錯得來的。具體為什麼是1.08,暫時還不知道 91 if (distance > 1.08f && cursorState) 92 { 93 transform.animation.Play("run"); //奔跑 94 95 //控制向量長度在 speed 範圍在內 96 Vector3 setp = Vector3.ClampMagnitude(mousePos - transform.position, speed); 97 //charController.Move(setp*0.3f); //開始我用這個方法。不是我想要的效果 98 /* 99 這裡要區別於:Move() 一個更加複雜的運動函式,每次都絕對運動 100 * SimpleMove:以一定的速度移動角色 101 */ 102 //cursorState = false; 103 charController.SimpleMove(setp); 104 105 } 106 else 107 { 108 if (x == 0 && y == 0) 109 { 110 //transform.animation.Play("idle"); 111 cursor.SetActive(false); 112 //cursorState = false; 113 if (!transform.animation.IsPlaying("attack"))//當前沒有攻擊 114 transform.animation.Play("idle"); 115 } 116 //transform.animation.Play("idle"); 117 } 118 //右鍵攻擊 119 /* 120 GetMouseButton:按下不彈起就一直執行 121 * GetMouseButtonDown:按下執行一次。 122 * 123 */ 124 if (Input.GetMouseButton(1)) 125 { 126 cursorState = true; 127 transform.animation.Play("attack");//攻擊 128 } 129 //transform.animation.Play("run"); //奔跑 130 //設定角色面朝的方向 131 //Vector3 lookPos = transform.position - beforePos + transform.position; 132 //transform.LookAt(lookPos); 133 134 135 //設定相機跟隨 136 Vector3 camera = Camera.main.transform.position; //獲取主相機的位置 137 Vector3 currChart = transform.position;//當前角色位置 138 139 camera.x = currChart.x; 140 camera.y = currChart.y + 5; 141 camera.z = currChart.z - 10; 142 143 Camera.main.transform.position = camera; 144 145 //if ((charController.collisionFlags & CollisionFlags.Above) != 0) 146 // print("touched the ceiling"); 147 148 } 149 /// <summary> 150 /// 當控制器碰撞一個正在運動的碰撞器時,OnControllerColliderHit 被呼叫。 151 /// </summary> 152 /// <param name="hit"></param> 153 public void OnControllerColliderHit(ControllerColliderHit hit) 154 { 155 156 //如果碰到的是牆並且boos is die,進入下一關,這裡 牆 代替關卡 157 if (hit.transform.name == "wall") 158 { 159 //載入關卡,2 表示Build Settings中場景的索引 160 Application.LoadLevel(2); 161 } 162 } 163 164 public void OnTriggerEnter(Collider other) 165 { 166 167 } 168 169 }
複製程式碼