unity第一人稱如何設定
阿新 • • 發佈:2020-07-12
關係圖
紅色菱形:指令碼
白色矩形:元件
移動程式碼
//移動程式碼
public CharacterController controller;//角色控制器
public float speed = 12f;//移動速度
public float gravity = -20f;//重力加速度
public float jumpHeight = 3f;//跳躍高度 public Transform groundCheck;//碰撞檢測物體(空物體),模型底部的東西 , 用於檢測是否落地
public float groundDistance = 0.4f;//半徑大小public LayerMask groundMask;//被檢測的被碰撞物體 Vector3 velocity;//移動速度
bool isGrounded;//腳底是否與地面接觸 private Animator animator;
private void Start()
{
animator = this.GetComponentInChildren<Animator>(); } float t;
// Update is called once per frame
void Update(){
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); if (isGrounded && velocity.y < )
{
velocity.y = -2f;
} if (Input.GetButtonDown("Jump") && isGrounded)
{ velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); } float x = Input.GetAxis("Horizontal");float z = Input.GetAxis("Vertical"); Vector3 move = transform.right * x + transform.forward * z; controller.Move(move * speed * Time.deltaTime); velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime);
}
視角程式碼
//視角程式碼
public float 滑鼠靈敏度 = 100f;
public Transform 玩家;
float xRoation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
} // Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * 滑鼠靈敏度 * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * 滑鼠靈敏度 * Time.deltaTime; xRoation -= mouseY;
xRoation = Mathf.Clamp(xRoation, -75f, 75f); transform.localRotation = Quaternion.Euler(xRoation, 0f, 0f);
玩家.Rotate(Vector3.up * mouseX);
}