touch count

本文介绍如何在Unity中实现角色的行走控制。通过检测触摸输入并使用射线投射来确定角色的目标位置,然后使角色朝该位置移动。文中提供了具体的C#代码实现。

几天在QQ群,有人问了怎么实现人物的行走控制,正好之前做过一个游戏的小DEMO,控制一个玩家接受任务,然后去副本打怪,就有实现这功能。

分享一下相关代码吧,呵呵,自己参照Unity的demo写的不涉及其他...

[csharp]  view plain copy
  1. private void TouchControl() {  
  2.         if (state != STATE_DIALOG && state != STATE_DIE) {  //如果角色不在对话状态/死亡状态,才能移动  
  3.             int touchCount = 0;  
  4.             if (touchCount < Input.touchCount) {  
  5.                 Vector2 touchPosition = Input.GetTouch(touchCount).position;  
  6.                 touchPosition.y = 480 - touchPosition.y;  
  7.   
  8.                 //如果是单击或者是滑动  
  9.                 if (Input.GetMouseButtonDown(0) || Input.GetTouch(touchCount).phase == TouchPhase.Moved ) {  
  10.                     Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);  
  11.                     Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);  // 不放心的话,画出这条射线看看  
  12.                     RaycastHit hit;  //用来从一个raycast后获取信息。  
  13.   
  14.   
  15.                     if (Physics.Raycast(ray, out hit)) {   //如果射线发生碰撞返回true  
  16.                         float touchDist = (transform.position - hit.point).magnitude; //返回向量的长度  
  17.                         if (touchDist > 0.1) {  
  18.                             targetLocation = hit.point;  
  19.                             state = STATE_MOVING;  
  20.                             targetCircle.transform.position = hit.point;  
  21.                         }  
  22.                     }  
  23.                 }  
  24.             }  
  25.         }  
  26. // 以上代码,主要是获取一个目标点,有了目标点,我们就要让玩家行走移动了。  
  27.   
  28.         else if (state == STATE_MOVING) {  
  29.             Vector3 movement = Vector3.zero;  
  30.             movement = targetLocation - transform.position;  
  31.             movement.y = 0;  
  32.             float dist = movement.magnitude;  
  33.   
  34.             if (dist < 0.1) {  
  35.                 state = STATE_STAND;  
  36.             }  
  37.             else {  
  38.                 movement = movement.normalized * speed;  
  39.             }  
  40.             movement += velocity;  
  41.             movement += Physics.gravity;  
  42.             movement *= Time.deltaTime;  
  43.   
  44.             character.Move(movement);  
  45.             FaceMovementDirection();  
  46.         }  
  47.   
  48.     //控制角色朝向移动方向  
  49.     private void  FaceMovementDirection (){   
  50.    Vector3 horizontalVelocity = character.velocity;  
  51.    horizontalVelocity.y = 0; //忽略垂直移动  
  52.    if ( horizontalVelocity.magnitude > 0.1f )  
  53.             player.forward = horizontalVelocity.normalized;  //控制玩家朝向  
  54.     }  
using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class TouchScreen : MonoBehaviour { private Touch oldTouch1; //上次触摸点1(手指1) private Touch oldTouch2; //上次触摸点2(手指2) Touch newTouch1; Touch newTouch2; public Edit edit; public bool top = false; private Vector3 dragOffset; private bool isTouchDragging = false; // Start is called before the first frame update void Start() { } //public void OnPointerClick(PointerEventData eventData) //{ // Debug.Log("UI被点击: " + gameObject.name); // edit.SelectedObj = this.gameObject; // 处理点击事件 //} void OnMouseDown() { edit.can = true; GameObject topObject = GetTopmostClickedObject(); edit.SelectedObj = this.gameObject; } private GameObject GetTopmostClickedObject() { // 创建指针事件数据 PointerEventData eventData = new PointerEventData(EventSystem.current); eventData.position = Input.mousePosition; // 存储所有射线检测结果 List<RaycastResult> results = new List<RaycastResult>(); // 执行射线检测(包含UI和3D/2D物体) EventSystem.current.RaycastAll(eventData, results); // 如果没有命中任何物体,返回null if (results.Count == 0) return null; // 根据深度排序结果(深度值越小表示越靠前) results.Sort((a, b) => a.depth.CompareTo(b.depth)); // 调试输出所有命中的物体(可选) foreach (var result in results) { // Debug.Log("命中物体: " + result.gameObject.name + " | 深度: " + result.depth); } // 返回最上层物体(深度值最小的) return results[0].gameObject; } // Update is called once per frame void Update() { if (edit.can) { //没有触摸 if (Input.touchCount <= 0) { return; } //单点拖动 else if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) { // var deltaposition = Input.GetTouch(0).deltaPosition; edit.SelectedObj.transform.Translate(new Vector3(deltaposition.x * 0.005f, deltaposition.y * 0.005f, 0f), Space.World); } //双指旋转 && 缩放 else if (2 <= Input.touchCount) { //Debug.Log("单点触摸, 水平上下旋转 "); Touch touch = Input.GetTouch(0); Vector2 deltaPos = touch.deltaPosition;//位置增量 if (Mathf.Abs(deltaPos.x) >= 3 || Mathf.Abs(deltaPos.y) >= 3) { edit.SelectedObj.transform.Rotate(Vector3.forward * deltaPos.x * Time.deltaTime*5, Space.World);//绕y轴旋转 //transform.Rotate(Vector3.right * deltaPos.y, Space.World);//绕x轴 } //缩放 newTouch1 = Input.GetTouch(0);// newTouch2 = Input.GetTouch(1); if (newTouch2.phase == TouchPhase.Began) { oldTouch2 = newTouch2; oldTouch1 = newTouch1; return; } float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);//计算两点距离 float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);//计算两点距离 float offset = newDistance - oldDistance; if (Mathf.Abs(offset) >= 3) { Debug.Log(offset); //放大因子, 一个像素按 0.01倍来算(100可调整) float scaleFactor = offset / 400f; Vector3 localScale = edit.SelectedObj.transform.localScale; Vector3 scale = new Vector3(localScale.x + scaleFactor, localScale.y + scaleFactor, localScale.z + scaleFactor); //最小缩放到 0.3 倍 最大1.5倍 if (scale.x > 0.3f && scale.x < 1.5f) { edit.SelectedObj.transform.localScale = scale;//赋新值 } //记住最新的触摸点,下次使用 oldTouch1 = newTouch1; oldTouch2 = newTouch2; } } } } } unity 使用以上代碼,將單指移動變爲滑鼠拖拽使使用滑鼠也能移動
最新发布
11-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值