Unity中玩家通过点击行走 或 滑动屏幕行走的实现
来源:互联网 发布:优酷视频解析 知乎 编辑:程序博客网 时间:2024/05/01 11:20
前几天在QQ群,有人问了怎么实现人物的行走控制,正好之前做过一个游戏的小DEMO,控制一个玩家接受任务,然后去副本打怪,就有实现这功能。
分享一下相关代码吧,呵呵,自己参照Unity的demo写的不涉及其他...
private void TouchControl() { if (state != STATE_DIALOG && state != STATE_DIE) { //如果角色不在对话状态/死亡状态,才能移动 int touchCount = 0; if (touchCount < Input.touchCount) { Vector2 touchPosition = Input.GetTouch(touchCount).position; touchPosition.y = 480 - touchPosition.y; //如果是单击或者是滑动 if (Input.GetMouseButtonDown(0) || Input.GetTouch(touchCount).phase == TouchPhase.Moved ) { Ray ray = mainCam.ScreenPointToRay(Input.mousePosition); Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow); // 不放心的话,画出这条射线看看 RaycastHit hit; //用来从一个raycast后获取信息。 if (Physics.Raycast(ray, out hit)) { //如果射线发生碰撞返回true float touchDist = (transform.position - hit.point).magnitude; //返回向量的长度 if (touchDist > 0.1) { targetLocation = hit.point; state = STATE_MOVING; targetCircle.transform.position = hit.point; } } } } }// 以上代码,主要是获取一个目标点,有了目标点,我们就要让玩家行走移动了。 else if (state == STATE_MOVING) { Vector3 movement = Vector3.zero; movement = targetLocation - transform.position; movement.y = 0; float dist = movement.magnitude; if (dist < 0.1) { state = STATE_STAND; } else { movement = movement.normalized * speed; } movement += velocity; movement += Physics.gravity; movement *= Time.deltaTime; character.Move(movement); FaceMovementDirection(); } //控制角色朝向移动方向 private void FaceMovementDirection (){ Vector3 horizontalVelocity = character.velocity; horizontalVelocity.y = 0; //忽略垂直移动 if ( horizontalVelocity.magnitude > 0.1f ) player.forward = horizontalVelocity.normalized; //控制玩家朝向 }
粗略的贴了点代码,琢磨一下,实现起来应该不难~~
也当是记一下笔记,以后备用。
- Unity中玩家通过点击行走 或 滑动屏幕行走的实现
- Unity中玩家通过点击行走 或 滑动屏幕行走的实现
- unity之人物行走的实现
- 【行走】
- 行走
- 行走
- 行走
- 行走
- unity3d 鼠标点击或触摸 控制角色对象行走
- unity3d 鼠标点击或触摸 控制角色对象行走
- unity3d 鼠标点击或触摸 控制角色对象行走
- Unity 实现类似 QQ泡泡堂一样平滑行走的 基于Tile的移动算法
- 使用canvas实现行走的小人动画
- unity 中基于NGUI中实现引导,拦截玩家的点击动作
- 行走于 Swift 的世界中
- 游戏中角色曲线行走的算法
- [IOS]行走于Swift的世界中
- AndEngine屏幕跟随人物行走
- Nginx和Tomcat负载均衡实现session共享
- 为自己的应用程序添加任务栏托盘功能
- 动态连接5-readelf -h -l file_b.so
- 基于51单片机的大气温度采集和记录系统
- ABAP - OLE excel download
- Unity中玩家通过点击行走 或 滑动屏幕行走的实现
- 基于51单片机多路温度采集系统 采用读取ds18b20的序列号进行测量
- 基础的Linux常用命令
- select 挡住 div的解决方法
- 动态连接6-readelf -h -l hello
- 嵌入式GUI RT-THREAD/FTK-0.6发布
- ubuntu linux 常用小技巧
- 《ASP.NET本质论》MVC处理程序
- 测试某个目录中文件是否存在