素材:
https://share.weiyun.com/5vZ22uQ
导入素材,创建平面Plane,贴图,调整Shader参数等如图:
添加空物体,名称改为PlayerHandle,添加胶囊碰撞盒,调整位置,再通过素材里的model添加子物体ybot
PlayerHandel挂载两个代码文件,一个为PlayerInput,另一个叫ActorController
PlayerInput负责处理输入,使之转化为有效的输出信号
PlayerInput:
前后左右的平滑移动:
targetDup = (Input.GetKey(KeyUp)?1.0f:0) - (Input.GetKey(KeyDown)?1.0f:0) ;
targetDright = (Input.GetKey(KeyRight) ? 1.0f : 0) - (Input.GetKey(KeyLeft) ? 1.0f : 0);//目的实现双轴信号
Dup = Mathf.SmoothDamp(Dup, targetDup, ref VelocityDup, 0.1f);//ref 这个参数不管 该方法自己选的
Dright = Mathf.SmoothDamp(Dright, targetDright, ref VelocityDright, 0.1f);//平滑修改值
由于Dup和Dright可以同时为1,在决定移动速度的时候,需要一个函数将Dup和Dright结合起来,避免在走动状态下,斜着移动更快,所以我们需要将直角坐标改为圆形坐标
解决办法 论文: https://arxiv.org/ftp/arxiv/papers/1509/1509.06344.pdf
采用论文第五页 Elliptical Grid Mapping
Vector2 tempDAxis = SquareToCircle(new Vector2(Dright, Dup));//为了解决斜走直接跑
float Dright2 = tempDAxis.x;
float Dup2 = tempDAxis.y;
Dmag = Mathf.Sqrt((Dup2 * Dup2) + (Dright2 * Dright2));//简单用原本的Dup Dright开方会导致斜着走的时候跑
Dvec = Dright2 * transform.right + Dup2 * transform.forward;
private Vector2 SquareToCircle (Vector2 input)//Elliptical Grid Mapping 有点像椭圆
{
Vector2 output = Vector2.zero;
output.x = input.x * Mathf.Sqrt(1 - (input.y * input.y) / 2.0f);
output.y = input.y * Mathf.Sqrt(1 - (input.x * input.x) / 2.0f);
return output;
}
处理Jump信号:
bool newJump = Input.GetKey(keyB);//尝试用GetKeyDown
if (newJump != lastJump && newJump==true)
{
jump = true;
}
else
{
jump = false;
}
lastJump = newJump;
此外 还应该做一个软开关,使得能够容易锁定和解锁用户输入
if (!inputEnabled)//制作软开关 soft
{
targetDup = 0;
targetDright = 0;
}
来源:CSDN
作者:冯雪娟
链接:https://blog.csdn.net/qq_17622999/article/details/104697778