Unity控制角色移动
Copy代码前的准备工作
整体结构
建立个空对象命名随意,主要是为了:角色掉出地图时,可以回到空对象的位置
在建立个空对象命名为Player,放到PlayResurrectionPoints子级下,并把Tag设置为Player,方便后续工作
把模型放入Player子级下,我就使用胶囊模型当作玩家
我们还需要建立个Camera当作玩家的眼睛,建立在Player下,这还没完,我们还需要把它Tag设置成PlayerCamera
哈哈,找不到是吧,因为我们的项目没有添加,我们需要自己添加,点击Add Tag…
到了这个页面,我们点击右下角的+按钮
输入PlayerCamera,输入完成后点击Save按钮,即可
这些工作都完成了,新建个脚本,自己随便命名,就可以Copy代码下面的完整代码了
完整代码
CharacterController playerController;
Vector3 direction;
[Tooltip("移动速度")]
public float Movespeed = 3f;
[Tooltip("冲刺速度")]
public float Sprintspeed = 5f;
[Tooltip("跳跃高度")]
public float jumpPower = 2.2f;
[Tooltip("重力")]
public float gravity = 7f;
[Tooltip("鼠标速度")]
public float mousespeed = 3f;
[Tooltip("最高落下不扣血")]
public float MaxFallNoDamage = 3f;
[Tooltip("限制最小角度")]
public float minmouseY = -60f;
[Tooltip("限制最大角度")]
public float maxmouseY = 65f;
GameObject PlayResurrectionPoints;
float RecordMovespeed;
float Recordgravity;
float RotationY = 0f;
float RotationX = 0f;
float RecordHeight;
Transform agretctCamera;
Vector2 m_screenpos = new Vector2();
// Use this for initialization
void Start()
{
RecordMovespeed = Movespeed;
Recordgravity = gravity;
playerController = GetComponent<CharacterController>();
agretctCamera = GameObject.FindWithTag("PlayerCamera").GetComponent<Transform>();
PlayResurrectionPoints = GameObject.Find("PlayResurrectionPoints");
//Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate()
{
if(Input.touchCount == 1)
{
if(Input.touches[0].phase == TouchPhase.Began)
{
m_screenpos = Input.touches[0].position;
}
}
//GameObject.Find()
Debug.Log(m_screenpos);
float _horizontal = Input.GetAxis("Horizontal");
float _vertical = Input.GetAxis("Vertical");
if (playerController.isGrounded)
{
direction = new Vector3(_horizontal, 0, _vertical);
if (Input.GetKeyDown(KeyCode.Space))
direction.y = jumpPower;
if(RecordHeight - transform.localPosition.y > MaxFallNoDamage)
{
//这里可以写摔伤的函数
//这个是得出超出高度的值
Debug.Log((RecordHeight - transform.localPosition.y) - MaxFallNoDamage);
}
RecordHeight = 0;
}
direction.y -= gravity * Time.deltaTime;
playerController.Move(playerController.transform.TransformDirection(direction * Time.deltaTime * Movespeed));
RotationX += agretctCamera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousespeed;
RotationY -= Input.GetAxis("Mouse Y") * mousespeed;
RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY);
this.transform.eulerAngles = new Vector3(0, RotationX, 0);
agretctCamera.transform.eulerAngles = new Vector3(RotationY, RotationX, 0);
//冲刺
if (Input.GetKey(KeyCode.W) && Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKey(KeyCode.W) && Input.GetKeyDown(KeyCode.RightShift))
{
if (Movespeed != Sprintspeed)
{
Movespeed = Sprintspeed;
gravity = 12f;
}
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.W) || Input.GetKey(KeyCode.RightShift) && Input.GetKeyDown(KeyCode.W))
{
if (Movespeed != Sprintspeed)
{
Movespeed = Sprintspeed;
gravity = 12f;
}
}
if (Input.GetKeyUp(KeyCode.W) && Input.GetKey(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.W) && Input.GetKey(KeyCode.RightShift))
{
Movespeed = RecordMovespeed;
gravity = Recordgravity;
}
if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
{
Movespeed = RecordMovespeed;
gravity = Recordgravity;
}
//如果玩家掉入深渊回到复活点
if (gameObject.transform.localPosition.y < -50)
{
gameObject.transform.position = new Vector3(PlayResurrectionPoints.transform.position.x, PlayResurrectionPoints.transform.position.y, PlayResurrectionPoints.transform.position.z);
}
//记录高度,玩家是否摔伤
if(transform.localPosition.y > RecordHeight)
{
RecordHeight = transform.localPosition.y;
}
}
最后工作
将脚本放到Player身上,在建立个Character Controller,最后就可以运行了,这些代码都是最基础的功能,我们可以继续把代码完善好
本篇技术文章完成了,如果觉得本技术文章对你有帮助请给我点个赞,如果有什么不足的地方,给我提意见,让我加以改进