Unity实现一个简单的人物控制
今天来实现一个简单的人物移动的方法。根据用户输入的方向旋转角色并向前移动
具体的实现是这样的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float rotateSpeed=5f;
public float moveSpeed=5f;
void Update()
{
float hor= Input.GetAxis("Horizontal"); //获取用户水平轴输入
float vor=Input.GetAxis("Vertical"); //获取用户垂直轴输入
if(hor!=0||vor!=0)//当用户的输入不为零时才调用移动旋转的方法
{
MovementRotation(hor,vor);
}
}
private void MovementRotation(float hor,float vor)
{
Quaternion dir=Quaternion.LookRotation(new Vector3(hor,0,vor));
this.transform.rotation=Quatenion.Lerp(this.transform.rotation,dir,rotateSpeed * Time.deltaTime);
//使用Lerp方法可以是让旋转更加平滑
this.transform.Translate(0,0,moveSpeed * Time.deltaTime);//人物向前移动的方法
}
}
此方法可以实现简单的人物移动,呃。。。。这是我第一次写博客,所以还不会把做出来的效果放上来,大家有兴趣可以自己去试一试这个方法,配合上动画的话就基本上完美了。
以后我会陆续写一些我在学习Unity中遇到了一些问题或者知识点,也希望我写的不对的地方大佬们可以给我指出,互相学习互相进步吧~~