日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Unity基础】人物控制的三种方式(键盘)、(鼠标)、(键鼠)

發(fā)布時間:2024/3/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Unity基础】人物控制的三种方式(键盘)、(鼠标)、(键鼠) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

先看效果:

(鍵鼠):

人物會隨著指針轉動,WASD控制人物移動。

鼠標

鍵盤就是去除掉鍵鼠的看向鼠標指針,改成看向最后方向

代碼:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using UnityEngine.EventSystems; public enum EControlType {KeyBrd,Mouse,KeyMouse } public class PlayerController : MonoBehaviour {Animator animator;NavMeshAgent agent;public float speed = 5.0f;public AudioClip clipFoot;EControlType ControlType;bool isKeyMouse;void Start(){animator = GetComponent<Animator>();agent = GetComponent<NavMeshAgent>();agent.speed = speed;}void Update(){//按鍵float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Vector3 dir = new Vector3(h, 0, v);dir = Camera.main.transform.TransformDirection(dir);dir.y = 0;if (dir.magnitude > 0.1f){ControlType = EControlType.KeyBrd;agent.isStopped = true;}transform.Translate(dir * speed * Time.deltaTime, Space.World);//網格導航尋路transform.LookAt(dir + transform.position);if (EventSystem.current.IsPointerOverGameObject()){return;}if (agent && Input.GetMouseButtonDown(1)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if (Physics.Raycast(ray, out hit, 1 << 6)){agent.destination = hit.point;ControlType = EControlType.Mouse;agent.isStopped = false;}}//鼠標加鍵盤if (agent){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;// 第三個參數(shù):距離,第四個:碰撞器 if (Physics.Raycast(ray, out hit, 100f, 1 << 6)){if (Input.GetKeyDown(KeyCode.Space)){isKeyMouse = !isKeyMouse;}if (isKeyMouse){ControlType = EControlType.KeyMouse;transform.LookAt(hit.point);agent.isStopped = false;}}}//二 動畫和位移 切換方式switch (ControlType){case EControlType.KeyBrd:transform.Translate(dir * speed * Time.deltaTime, Space.World);animator.SetFloat("Speed", dir.magnitude);transform.LookAt(transform.position + dir);break;case EControlType.Mouse:float curSpeed = agent.velocity.magnitude / speed;print("curSpeed= " + curSpeed);animator.SetFloat("Speed", curSpeed);break;case EControlType.KeyMouse:transform.Translate(dir * speed * Time.deltaTime, Space.World);animator.SetFloat("Speed", dir.magnitude);break;}}void playFootSound(){AudioSource.PlayClipAtPoint(clipFoot, transform.position);}public void normalAttack(int index){animator.SetInteger("NormalAttack", index);}}

總結

以上是生活随笔為你收集整理的【Unity基础】人物控制的三种方式(键盘)、(鼠标)、(键鼠)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。