1 Star 0 Fork 0

杨文泽/杨文泽的各种作业

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
Player.cs 2.28 KB
Copy Edit Raw Blame History
杨文泽 authored 2022-11-03 09:18 . 第一人称
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* @Author: 杨文泽
* @Description:
* @Date: 2022年11月03日 星期四 16:11:03
* @Modify:
*/
public class Player : MonoBehaviour
{
public float speed = 5f;
// Start is called before the first frame update
void Start()
{
//隐藏鼠标指针
Cursor.visible = false;
//隐藏鼠标指针到屏幕中央
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
Move();
MouseLook();
}
void Move()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//方向永远平行地面,角色不能早到天上去
//获得角色前方向量,将y轴分量设为0
Vector3 fwd = transform.forward;
Vector3 f = new Vector3(fwd.x, 0, fwd.z).normalized;
//角色的右方向量与右方向的移动直接对应,与抬头无关,可以直接使用
Vector3 r = transform.right;
//用f和r作为向量的基,组成移动向量
Vector3 move = f * z + r * x;
//直接改变玩家位置
transform.position += move * speed * Time.deltaTime;
}
void MouseLook()
{
float mx = Input.GetAxis("Mouse X");
float my = -Input.GetAxis("Mouse Y");
Quaternion qx = Quaternion.Euler(0, mx, 0);
Quaternion qy = Quaternion.Euler(my, 0,0);
transform.rotation = qx * transform.rotation;
transform.rotation = transform.rotation * qy;
//angle是角色的俯仰角度
float angle = transform.eulerAngles.x;
// 使用欧拉角时,经常出现-1°和359°混乱等情况,下面这些情况加以处理
if (angle > 180)
{
angle -= 360;
}
if (angle < 180)
{
angle += 360;
}
//限制抬头低头角度
if(angle > 80)
{
transform.eulerAngles = new Vector3(80, transform.eulerAngles.y, 0);
}
if (angle < -80)
{
transform.eulerAngles = new Vector3(-80, transform.eulerAngles.y, 0);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/yangwenze717/yang-wenzes-gez-homework.git
git@gitee.com:yangwenze717/yang-wenzes-gez-homework.git
yangwenze717
yang-wenzes-gez-homework
杨文泽的各种作业
master

Search

0d507c66 1850385 C8b1a773 1850385