unity第一人称如何设置
Posted i1111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity第一人称如何设置相关的知识,希望对你有一定的参考价值。
关系图
红色菱形:脚本
移动代码
1 //移动代码 2 public CharacterController controller;//角色控制器 3 public float speed = 12f;//移动速度 4 public float gravity = -20f;//重力加速度 5 public float jumpHeight = 3f;//跳跃高度 6 7 public Transform groundCheck;//碰撞检测物体(空物体),模型底部的东西 , 用于检测是否落地 8 public float groundDistance = 0.4f;//半径大小 9 public LayerMask groundMask;//被检测的被碰撞物体 10 11 Vector3 velocity;//移动速度 12 bool isGrounded;//脚底是否与地面接触 13 14 private Animator animator; 15 private void Start() 16 { 17 animator = this.GetComponentInChildren<Animator>(); 18 19 } 20 21 float t; 22 // Update is called once per frame 23 void Update() 24 { 25 isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); 26 27 if (isGrounded && velocity.y < 0) 28 { 29 velocity.y = -2f; 30 } 31 32 if (Input.GetButtonDown("Jump") && isGrounded) 33 { 34 35 36 velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); 37 38 } 39 40 41 42 float x = Input.GetAxis("Horizontal"); 43 float z = Input.GetAxis("Vertical"); 44 45 46 Vector3 move = transform.right * x + transform.forward * z; 47 48 controller.Move(move * speed * Time.deltaTime); 49 50 velocity.y += gravity * Time.deltaTime; 51 52 controller.Move(velocity * Time.deltaTime); 53 }
视角代码
1 //视角代码 2 public float 鼠标灵敏度 = 100f; 3 public Transform 玩家; 4 float xRoation = 0f; 5 // Start is called before the first frame update 6 void Start() 7 { 8 Cursor.lockState = CursorLockMode.Locked; 9 } 10 11 // Update is called once per frame 12 void Update() 13 { 14 float mouseX = Input.GetAxis("Mouse X") * 鼠标灵敏度 * Time.deltaTime; 15 float mouseY = Input.GetAxis("Mouse Y") * 鼠标灵敏度 * Time.deltaTime; 16 17 xRoation -= mouseY; 18 xRoation = Mathf.Clamp(xRoation, -75f, 75f); 19 20 transform.localRotation = Quaternion.Euler(xRoation, 0f, 0f); 21 玩家.Rotate(Vector3.up * mouseX); 22 }
以上是关于unity第一人称如何设置的主要内容,如果未能解决你的问题,请参考以下文章