赞
踩
第一人称视角操作
此功能主要是借鉴YouTube上一个博主的教程,写的蛮好我就搬运过来让大家也试试。
其原理是:左右旋转是控制父物体的欧拉角的Y轴
transform.Rotate(0, Input.GetAxis("Mouse X") * sensetivity,0);
上下点头是子物体相机的欧拉角Z轴
playerCamera.Rotate(-Input.GetAxis("Mouse Y") * sensetivity, 0, 0);
水平方向
- moveDirection = new Vector3(Input.GetAxis("Horizontal")*moveSpeed, moveDirection.y, Input.GetAxis("Vertical")*moveSpeed);
- moveDirection = transform.TransformDirection(moveDirection);
跳跃:
- if (controller.isGrounded)
- {
- if (Input.GetButton("Jump"))
- moveDirection.y = jumpForce;
- else
- moveDirection.y = 0;
- }
移动
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection*Time.deltaTime);
- using UnityEngine;
-
- public class PlayerController : MonoBehaviour
- {
- [SerializeField] private float moveSpeed = 6f;
- [SerializeField] private float jumpForce = 10;
- [SerializeField] private float gravity = 15f;
-
- private Vector3 moveDirection;
-
- [Header("Camera details")]
- public float sensetivity = 1f;
- private Transform playerCamera;
- private CharacterController controller;
-
- void Start()
- {
- controller = GetComponent<CharacterController>();
- playerCamera = GetComponentInChildren<Camera>().transform;
-
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
-
- void Update()
- {
- transform.Rotate(0, Input.GetAxis("Mouse X") * sensetivity,0);
- playerCamera.Rotate(-Input.GetAxis("Mouse Y") * sensetivity, 0, 0);
- if (playerCamera.localRotation.eulerAngles.y != 0)
- playerCamera.Rotate(Input.GetAxis("Mouse Y")*sensetivity,0,0);
-
-
- moveDirection = new Vector3(Input.GetAxis("Horizontal")*moveSpeed, moveDirection.y, Input.GetAxis("Vertical")*moveSpeed);
- moveDirection = transform.TransformDirection(moveDirection);
-
- if (controller.isGrounded)
- {
- if (Input.GetButton("Jump"))
- moveDirection.y = jumpForce;
- else
- moveDirection.y = 0;
- }
-
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection*Time.deltaTime);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。