gpt4 book ai didi

c# - 有没有更好的移动方式?

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:06 24 4
gpt4 key购买 nike

现在我是这样移动的:

foreach (var touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
startPos = touch.position;
endPos = touch.position;
}
if (touch.phase == TouchPhase.Moved) {
endPos = touch.position;
}

if(touch.phase == TouchPhase.Ended){
//Left
if ((startPos.x - endPos.x) > 30) {
Move (new Vector3 (-1, 0, 0));
}
//Right
else if ((endPos.x - startPos.x) > 30) {
Move (new Vector3 (1, 0, 0));
}
//Down
else if ((startPos.y - endPos.y) > 30) {
Move (new Vector3 (0, 0, -1));
}
//Up
else {
Move (new Vector3 (0, 0, 1));
}
}
}

这在大多数情况下都有效,但有时会走错方向。我想通过向右滑动向右移动,向左滑动向左移动,向下滑动向后移动,向前移动或点击向前移动。有更好的方法吗?

最佳答案

如果您只想根据一次 触摸移动,那么只需实现Unity 的example ,并且在表示 if (directionChosen) 的位中,您只需放置类似以下的内容来处理点击前进。

    if (directionChosen) {
Vector3 desiredVector = Vector3.zero
if (direction.magnitude < 30){
desiredVector = new Vector3(0,0,1);
} else {
desiredVector = new Vector3(direction.x,0,direction.y).normalized;
}
Move(desiredVector);
}

但是,如果您想跟踪所有触摸,并在其中一个触摸抬起时移动变换,则需要更复杂的解决方案。这是实现一些函数式编程原则的好机会,主要是:closure (ctrl+f“闭包”)。

这将允许您为每个 Touch 创建一个独特的函数,以跟踪所有必要的变量。一个示例实现就像这样(它编译但我没有测试,因为我没有必要的设备。希望它传达了这个想法):

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchMover : MonoBehaviour {

private float _speed;
private Dictionary<int,Func<Touch, Vector3>> _touchFuncDict;

void Awake(){
_touchFuncDict = new Dictionary<int,Func<Touch, Vector3>>();
}

void Update(){
//Clean _touchFuncDict of any functions that reference a touch that is not active
int[] touchIds = (int[])Input.touches.Select( x => x.fingerId);
var keysToRemove = _touchFuncDict.Keys.Except(touchIds).ToList();
foreach (var key in keysToRemove)
_touchFuncDict.Remove(key);

// Iterate through touches, and evaluate it's associate GetTouchVelocity function.
// If we haven't created a function for it yet, then do so;
foreach (var touch in Input.touches){
Func<Touch,Vector3> getTouchVelocity = null;
if (!_touchFuncDict.TryGetValue(touch.fingerId, out getTouchVelocity)){
getTouchVelocity = GenerateTouchVelocityFunc(30);
_touchFuncDict.Add(touch.fingerId,getTouchVelocity);
}
transform.position = transform.position + getTouchVelocity(touch) * _speed * Time.deltaTime;
}
}

// Generates a function that requires a Touch parameter and will return a Vector3
Func<Touch, Vector3> GenerateTouchVelocityFunc(int buffer){
Vector2 startPos = Vector2.zero;
Vector2 moveDelta = Vector2.zero;
Vector3 desiredVelocity = Vector3.zero;

Func<Touch, Vector3> getTouchVelocity = delegate(Touch touch){
switch(touch.phase){
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Moved:
moveDelta = touch.position - startPos;
break;
case TouchPhase.Ended:
if (moveDelta.magnitude > buffer){
desiredVelocity = new Vector3(moveDelta.x,0,moveDelta.y).normalized;
} else {
// this is your move forward on tap;
desiredVelocity = new Vector3(0,0,1);
}
break;
}
return desiredVelocity;
};
return getTouchVelocity;
}

}

关于c# - 有没有更好的移动方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42470212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com