我是Unity的新手,我试图制作一个超级六边形游戏副本,但我对Input统一感到困惑。我为键盘使用了Input.GetAxis(“ Horizontal”),它确实返回了一个值,我只是使用它使用transform.RotateAround()围绕中间对象围绕播放器对象旋转。但是,当我尝试在移动设备上获取它时,我编写了一个脚本来确定它是SWIPE LEFT还是SWIPE RIGHT。那行得通。但是我不知道如何基于从-1到1的滑动返回值。这样我就可以使用这些值,有人可以帮忙。我也在这里添加我的脚本
public class Player : MonoBehaviour {
public GameManager gameManager;
public SwipeController swipe;
public float moveSpeed = 600f;
float movement = 0f;
// Update is called once per frame
void Update()
{
this.movement = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, this.movement * Time.fixedDeltaTime * -moveSpeed);
}
}
这里也是我的滑动脚本
public class SwipeController : MonoBehaviour
{
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDragging;
private Vector2 startTouch, swipeDelta;
private void Update()
{
tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
//stand Alone Inputs
if (Input.GetMouseButtonDown(0))
{
isDragging = true;
tap = true;
startTouch = Input.mousePosition;
} else if (Input.GetMouseButtonUp(0)) {
isDragging = false;
Reset();
}
//MOBILE INPUTS
if (Input.touches.Length > 0 )
{
if(Input.touches[0].phase == TouchPhase.Began)
{
tap = true;
isDragging = true;
startTouch = Input.touches[0].position;
} else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDragging = false;
Reset();
}
}
//Distance calcs
if (isDragging)
{
if(Input.touches.Length > 0)
{
swipeDelta = Input.touches[0].position - startTouch;
} else if (Input.GetMouseButton(0)) {
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
//DEAD ZONE?
if (swipeDelta.magnitude > 100)
{
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y))
{
//left or right
if (x < 0)
{
swipeLeft = true;
} else
{
swipeRight = true;
}
} else
{
// top or bottom
if (y < 0 )
{
swipeDown = true;
} else
{
swipeUp = true;
}
}
Reset();
}
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
}
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
}
答案 0 :(得分:0)
您可以使用类似这样的内容:
private float RemapFloat(float input, Vector2 refRange, Vector2 targetRange)
{
// map the input relative to the reference space
float normal = Mathf.InverseLerp(refRange.x, refRange.y, input);
// map the result to the target reference space
float outputValue = Mathf.Lerp(targetRange.x, targetRange.y, normal);
return outputValue;
}
这利用了:
然后,您可以这样命名,假设您要使用的“滑动值”是滑动距离:
// the mininum and maximum swipe distance range, could be based on your viewport size
Vector2 swipeValueRange = new Vector2(-swipeDistanceRange, swipeDistanceRange);
// this is the range of values you wish to use
Vector2 targetRange = new Vector2(-1f, 1f);
// the swipe value you wish to remap
float swipeValue = currentSwipeValue;
RemapFloat(swipeValue, swipeValueRange, targetRange);
答案 1 :(得分:0)
您可以使用Mathf.MoveTowards模拟从-1到1的值
伪代码:
float t;
if(SWIPE LEFT)
{
t = Mathf.MoveTowards(t, -1, STEP);
}
else if(SWIPE RIGHT)
{
t = Mathf.MoveTowards(t, 1, STEP);
}
else
{
t = Mathf.MoveTowards(t, 0, STEP);
}