滑鼠操作視角功能實作
1.創建一個空的物體(Create Empty)改名為Player, 添加一個膠囊體子物體, 這里使用主攝像機作為第一視角的攝像機, 所以將Main Camera拖到Player中作為子物體.Player添加角色控制器組件

將膠囊體的碰撞器移除, 因為角色控制器組件上有碰撞器
最后將攝像機放置在膠囊體合適的高度上

2.創建一個腳本MouseLook, 作為Main Camera的一個組件,用來實作滑鼠控制視角 ,
變數
public Transform player;//膠囊體和攝像機的父物體
public float mouseSensitivity = 100f;//滑鼠靈敏度
private float xRotation;//繞X軸旋轉引數,也就是處理滑鼠上下移動時用到的引數
Start函式
void Start() {
//隱藏滑鼠, 鎖定在螢屏中央
Cursor.lockState = CursorLockMode.Locked;
}
Update函式
//Input.GetAxis()函式獲取滑鼠滑動資訊,防止受幀率影響所以乘Time.deltaTime
float mouseX = Input.GetAxis("Mouse X")* mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y")* mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);//限制視角上下移動的角度, 上下的視角移動不能超過90度
//使用攝像機的區域坐標處理繞X軸旋轉, 換算成四元數
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
//繞Y軸旋轉
player.Rotate(Vector3.up * mouseX);
第一視角情況下,左右旋轉時角色也會跟著旋轉,而上下旋轉時角色位置不發生變化, 所以處理繞Y軸旋轉時(滑鼠左右移動)使用膠囊體和攝像機的父物體來控制整體的旋轉,X軸旋轉時(滑鼠上下移動)使用攝像機的區域坐標進行處理,
最后將滑鼠隱藏, 鎖定在螢屏中心
全部代碼
public class MouseLook : MonoBehaviour {
//關聯身體, 使滑鼠水平移動時, 身體也跟著移動
public Transform player;
//滑鼠靈敏度引數
public float mouseSensitivity = 100f;
private float xRotation;
void Start() {
//隱藏滑鼠, 鎖定在螢屏中央
Cursor.lockState = CursorLockMode.Locked;
}
void Update() {
//獲取滑鼠的移動資訊
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);//限制視角上下移動的角度, 上下的視角移動不能超過90度
//四元數換算
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
//繞著Y軸旋轉, 為了不受幀率的影響要乘以Time.deltaTime
player.Rotate(Vector3.up * mouseX);
}
}
角色移動和跳躍功能實作
創建一個腳本PlayerMove, 作為Player的一個組件,實作角色移動、跳躍、模擬重力、地面檢測,
變數
public CharacterController cc;
public Transform groundCheck;//膠囊體底部的檢測球體, 用來檢測是否在地面上
public float speed = 12f;//移動速度
public float gravity = -9.8f;//重力引數
public float checkDistance = 0.7f;//檢測球體半徑
public LayerMask ground;
public float jumpHeight = 2f;//跳躍高度
private Vector3 velocity;//模擬自由落體的向量
private bool isGrounded;//是否在地面上
Update函式
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//地面檢測
isGrounded = Physics.CheckSphere(groundCheck.position, checkDistance, ground);//通過檢測球檢測是否在地面上
print(isGrounded);//測驗用
//由于重力加速度一直在加, 所以當檢測到角色在地面上的時候加速度歸零, 由于角色模型可能在初始化的時候懸空, 所以設定為一個較小的負數確保角色會在地面上
if (isGrounded && velocity.y < 0) {
velocity.y = -2f;
}
//計算移動的向量
Vector3 move = transform.right * x + transform.forward * z;
//使用角色控制器的Move函式控制角色移動
cc.Move(move * speed * Time.deltaTime);
//實作跳躍
if (Input.GetButtonDown("Jump") && isGrounded){
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);//計算跳躍到jumpHeight高度需要的速度
}
//模擬重力
velocity.y += gravity * Time.deltaTime;
cc.Move(velocity * Time.deltaTime);//因為自由落體位移計算公式是S = 1/2 * g * t^2, 所以這里再次乘Time.deltaTime
Player再添加一個空的子物體GroundCheck, 放在膠囊體的最下面,然后將GroundCheck的位置關聯到groundCheck引數中,

全部代碼
public class PlayerMove : MonoBehaviour {
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3.0f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
private Vector3 velocity;
private bool isGrounded;
void Update() {
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
print(isGrounded);
if (isGrounded && velocity.y < 0) {
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/248170.html
標籤:其他
