目錄
- Character Controller組件
- 1. 移動
- 2. 重力和跳躍
- 2.1 重力
- 2.2 跳躍
- 實作第一人稱視角控制
Character Controller組件
官方檔案:https://docs.unity.cn/cn/2019.4/Manual/class-CharacterController.html
1. 移動
直接上代碼:
public class PlayerController : MonoBehaviour
{
//獲取組件
public CharacterController characterController;
//設定移動和跳躍速度
public float moveSpeed;
//定義按鍵輸入
public float getHorizontal, getVertical;
//定義移動向量
private Vector3 dir;
private void Start()
{
moveSpeed = 4;
characterController = GetComponent<CharacterController>();
}
private void Update()
{
getHorizontal = Input.GetAxis("Horizontal") * moveSpeed;
getVertical = Input.GetAxis("Vertical") * moveSpeed;
dir = transform.forward * getVertical + transform.right * getHorizontal;
characterController.Move(dir * Time.deltaTime);
}
}
2. 重力和跳躍
2.1 重力
- 定義重力、向下的速度(通過計算得出)
- 判斷物體是否在地面上,不在時開始計算向下的速度
- 通過physics.CheckSphere方法判斷物體是否在地面上
- 需要定義目標點transform、檢測半徑float、檢測圖層LayerMask來進行判斷
代碼:
public class PlayerController : MonoBehaviour
{
//定義重力和下落加速度
public float gravity = 9.8f;
private Vector3 velocity = Vector3.zero;
//定義bool值判斷角色是否在地面上
public bool isGround;
//定義一個目標點,一個檢測半徑,一個檢測圖層
public Transform checkGround;
public float checkRedius;
public LayerMask checkLayer;
private void Start()
{
checkRedius = 0.2f;
}
private void Update()
{
isGround = Physics.CheckSphere(checkGround.position, checkRedius, checkLayer);
if (isGround && isJumping < 0.1)
{
velocity.y = 0;
}
velocity.y -= gravity * Time.deltaTime;
characterController.Move(velocity * Time.deltaTime);
}
}
2.2 跳躍
在這里遇到一個困難,如果直接用以下代碼的話:
if (Input.GetButtonDown("Jump") && isGround)
{
velocity.y = jumpSpeed;
}
理論上可行,但是通過實踐發現根本跳不起來,猜測和在上一步添加重力時的這一段代碼有關:
if (isGround && isJumping < 0.1)
{
velocity.y = 0;
}
在起跳的時候程式又執行到這一部分,導致角色的velocity.y=0,從而使角色不能跳躍
暫時想到的解決辦法就是添加一個計時器,讓在起跳0.1秒內不再執行判斷isGround的代碼
通過修改后,PlayerController代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//獲取組件
public CharacterController characterController;
//設定移動速度
public float moveSpeed;
//定義按鍵輸入
public float getHorizontal, getVertical;
//定義移動向量
private Vector3 dir;
//定義跳躍速度和跳躍狀態
public float jumpSpeed;
public float isJumping;
//定義重力和下落加速度
public float gravity = 9.8f;
private Vector3 velocity = Vector3.zero;
//定義bool值判斷角色是否在地面上
public bool isGround;
//定義一個目標點,一個檢測半徑,一個檢測圖層
public Transform checkGround;
public float checkRedius;
public LayerMask checkLayer;
private void Start()
{
moveSpeed = 4;
jumpSpeed = 5;
checkRedius = 0.2f;
characterController = GetComponent<CharacterController>();
isJumping = 0.2f;
}
private void Update()
{
isGround = Physics.CheckSphere(checkGround.position, checkRedius, checkLayer);
if (isGround && isJumping < 0.1)
{
velocity.y = 0;
}
getHorizontal = Input.GetAxis("Horizontal") * moveSpeed;
getVertical = Input.GetAxis("Vertical") * moveSpeed;
dir = transform.forward * getVertical + transform.right * getHorizontal;
characterController.Move(dir * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGround)
{
velocity.y = jumpSpeed;
isJumping = 0.2f;
}
if(isJumping > 0)
{
isJumping -= Time.deltaTime;
Debug.Log(isJumping);
}
velocity.y -= gravity * Time.deltaTime;
characterController.Move(velocity * Time.deltaTime);
}
}
** 如果有關于玩家跳躍的更好解決辦法,歡迎發表在評論區 **
實作第一人稱視角控制
前面完成了前后左右移動和跳躍,這一步完成一個第一視角的轉動功能
將Camera拖到Player的節點下,調整位置,然后為Camera添加腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform Player;
private float mouseX, mouseY; //獲取滑鼠移動的值
public float mouseSensitivity; //滑鼠靈敏度
private float xRotation;
private void Start()
{
mouseX = 0;
mouseY = 0;
mouseSensitivity = 800;
}
private void Update()
{
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
if (xRotation > 90)
{
xRotation = 90;
}
else if (xRotation < -90)
{
xRotation = -90;
}
Player.Rotate(Vector3.up * mouseX);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
}
}
完成!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/518529.html
標籤:其他
下一篇:上架App Store的整體流程
