传奇广告查询第一站 同步54.com

Unity中使用C#脚本实现玩家前后左右移动
原创 于2026-01-05 18:07:00发布
12 阅读
0
0
  • 使用Quaternion(四元数)控制玩家移动
using System.Collections; using System.Collections.Generic; using UnityEngine; ///  /// Unity中使用C#脚本实现玩家前后左右移动 ///  public class CharcterMotor : MonoBehaviour { public float moveSpeed = 10; public float rotateSpeed = 50; private void Update() { //获取输入事件 float hor = Input.GetAxis("Horizontal"); float ver = Input.GetAxis("Vertical"); MovementRotateion(hor, ver); } //移动方法 private void MovementRotateion(float hor,float ver) { if (hor != 0 || ver != 0) { //生成转向方案 Quaternion dir = Quaternion.LookRotation(new Vector3(hor, 0, ver)); //转向方案给到玩家 Lerp函数:使转向更平滑 不会一下子闪过去 this.transform.rotation = Quaternion.Lerp(this.transform.rotation,dir,Time.deltaTime * rotateSpeed); //玩家向自己前方(朝向)移动 this.transform.Translate(0, 0, -Time.deltaTime * moveSpeed); } } } 
管理员
0
0
0
分享
上一篇: 批量去除图片的黑色背景,并且统一修改图片尺寸
下一篇: 陈伟霆电视剧《九门》开机 续写传奇热血新篇
评论
历史记录
回顶部
浏览时间 游戏名称 游戏IP 开区网址
注册GM1论坛账号
  • 上传头像
注册

已有账号,

微信扫码登录
重置密码
重置密码

注册

绑定关联手机号
关联手机号