超级简单系列--使用CharacterController控制人物移动

使用Input.GetAxis(“Horizontal”) 和 “Vertical”得到垂直和水平方向的值
使用CharacterController.SimpleMove(Vector3)参数表示运动的方向和速度 单位可以认为是 m/s
代码如下:

private CharacterController cc;
public float speed = 4;

void Start()
{
    cc = GetComponent<CharacterController>();
}


void Update()
{
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");
    if (Mathf.Abs(h)>0.1f||Mathf.Abs(v)>0.1)
    {
        Vector3 targetDir = new Vector3(h, 0, v);
        transform.LookAt(targetDir+transform.position);
        cc.SimpleMove(transform.forward * speed);
    }  
}

speed 是控制人物移动的速度
float h 获取的是操纵杆输入和键盘输入,值为(-1到1)的值,x轴正方向为1,负方向为-1,也就是说A键为-1,D键为1
float v获取的是操纵杆输入和键盘输入,值为(-1到1)的值,y轴正方向为1,负方向为-1,也就是说W键为1,S键为01
targetDir 是键盘输入之后获取到的方向,将目标用SimpleMove方法向获取到方向移动
transform.lookat 是让目标旋转到获取到的方向
transform.forward 是让目标向正前方移动


本文来自 恬静的小魔龙 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/q764424567/article/details/77984369?utm_source=copy

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值