C# 如何固定左右移动?
我花了很多时间试图找出代码中的问题,但是我找不到为什么我的代码没有被触发的解决方案。 在我以前的游戏中,当我实现这个代码时,它工作得非常好,而现在当我在新游戏中实现同样的触摸动作代码时,它就不工作了 我试着调试代码并将debug.Log放入Update方法中,当我在屏幕上滑动时,它甚至没有触发 代码如下:C# 如何固定左右移动?,c#,unity3d,game-development,C#,Unity3d,Game Development,我花了很多时间试图找出代码中的问题,但是我找不到为什么我的代码没有被触发的解决方案。 在我以前的游戏中,当我实现这个代码时,它工作得非常好,而现在当我在新游戏中实现同样的触摸动作代码时,它就不工作了 我试着调试代码并将debug.Log放入Update方法中,当我在屏幕上滑动时,它甚至没有触发 代码如下: int left = 0; int right = 0; int maxLeftCycles = 5; int maxRightCycles = 5; void Start() {
int left = 0;
int right = 0;
int maxLeftCycles = 5;
int maxRightCycles = 5;
void Start()
{
//touch
left = maxLeftCycles;
right = maxRightCycles;
}
private void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyUp(KeyCode.RightArrow) ||
Swipe.swipe == Swipe.SwipeDirection.right)
{
Swipe.ResetSwipe();
right = 0;
}
if (Input.GetKeyUp(KeyCode.LeftArrow) ||
Swipe.swipe == Swipe.SwipeDirection.left)
{
Swipe.ResetSwipe();
left = 0;
}
}
void FixedUpdate()
{
if (left < maxLeftCycles && !isMoving)
{
desiredPos = transform.position + Vector3.left * 1.52f;
isMoving = true;
left++;
}
if (right < maxRightCycles && !isMoving)
{
desiredPos = transform.position - Vector3.right * 1.52f;
isMoving = true;
right++;
}
if (isMoving)
{
transform.position = Vector3.MoveTowards(transform.position, desiredPos, moveSpeed * Time.deltaTime);
// this == is true if the difference between both
// vectors is smaller than 0.00001
if (transform.position == desiredPos)
{
isMoving = false;
transform.position = desiredPos;
}
}
}
这是滑动脚本的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swipe : MonoBehaviour
{
private float fingerStartTime = 0.0f;
private Vector2 fingerStartPos = Vector2.zero;
private bool isSwipe = false;
private float minSwipeDist = 50.0f;
private float maxSwipeTime = 0.5f;
public enum SwipeDirection
{
none,
up,
down,
right,
left
}
public static SwipeDirection swipe;
void Start()
{
swipe = SwipeDirection.none;
}
public static void ResetSwipe()
{
swipe = SwipeDirection.none;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began:
/* this is a new touch */
isSwipe = true;
fingerStartTime = Time.time;
fingerStartPos = touch.position;
break;
case TouchPhase.Canceled:
/* The touch is being canceled */
isSwipe = false;
break;
case TouchPhase.Ended:
float gestureTime = Time.time - fingerStartTime;
float gestureDist = (touch.position - fingerStartPos).magnitude;
if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
{
Vector2 direction = touch.position - fingerStartPos;
Vector2 swipeType = Vector2.zero;
if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
{
// the swipe is horizontal:
swipeType = Vector2.right * Mathf.Sign(direction.x);
}
else
{
// the swipe is vertical:
swipeType = Vector2.up * Mathf.Sign(direction.y);
}
if (swipeType.x != 0.0f)
{
if (swipeType.x > 0.0f)
{
// MOVE RIGHT
swipe = SwipeDirection.right;
}
else
{
// MOVE LEFT
swipe = SwipeDirection.left;
}
}
if (swipeType.y != 0.0f)
{
if (swipeType.y > 0.0f)
{
// MOVE UP
swipe = SwipeDirection.up;
}
else
{
// MOVE DOWN
swipe = SwipeDirection.down;
}
}
}
break;
}
}
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类刷卡:MonoBehavior
{
私人浮动fingerStartTime=0.0f;
私有向量2 fingerStartPos=Vector2.0;
私有布尔值=假;
私人浮标温度=50.0f;
专用浮点数maxSwipeTime=0.5f;
公共枚举开关定向
{
没有一个
向上的
下来,,
正确的,
左边
}
公共静态滑动方向滑动;
void Start()
{
滑动=滑动方向。无;
}
公共静态无效重置滑动()
{
滑动=滑动方向。无;
}
//每帧调用一次更新
无效更新()
{
如果(Input.touchCount>0)
{
foreach(输入触摸,触摸)
{
开关(接触相)
{
案例接触阶段。开始:
/*这是一种新的接触*/
isSwipe=true;
fingerStartTime=Time.Time;
fingerStartPos=触摸位置;
打破
案例处理阶段。已取消:
/*触摸被取消了*/
Issweep=false;
打破
案件审理阶段结束:
浮动手势时间=Time.Time-fingerStartTime;
float gestureDist=(touch.position-fingerStartPos).magnity;
如果(IsSweep&&gestureTimeMinsweepDist)
{
Vector2方向=touch.position-fingerStartPos;
Vector2 SwipType=Vector2.0;
如果(数学绝对值(方向x)>数学绝对值(方向y))
{
//横扫:
swipeType=Vector2.right*数学符号(direction.x);
}
其他的
{
//刷卡是垂直的:
swipeType=Vector2.up*数学符号(方向y);
}
如果(SwipType.x!=0.0f)
{
如果(SwipetType.x>0.0f)
{
//右移
swipe=SwipeDirection.right;
}
其他的
{
//向左移动
swipe=SwipeDirection.left;
}
}
如果(SwipType.y!=0.0f)
{
如果(SwipType.y>0.0f)
{
//上移
swipe=SwipeDirection.up;
}
其他的
{
//下移
滑动=滑动方向。向下;
}
}
}
打破
}
}
}
}
}
我调试的刷卡输入更新方法中的代码从未被调用或对我无效。我无法理解我做错了什么,因为相同的代码实际上在我以前的游戏中有效
非常感谢您阅读我的问题,我希望有人能帮助我解决这个问题 问题在于,你实际上对左右移动做了相同的事情: 左: 对:
desiredPos = transform.position - Vector3.right * 1.52f;
“+Vector3.left”与“-Vector3.right”相同,因为左侧为(-1,0,0),右侧为(1,0,0)。因此,在这两种情况下,你都会在你的位置上加上相同的向量(-1,0,0),因为向量3之前有加号。向左走,向量3之前有减号。向右走
因此,您需要在两种情况下使用相同的符号(加号或减号),或者使用相同的向量(右或左)如果代码根本没有触发,是否将脚本附加到要移动的对象上?您好。伙计,你完全改变了问题。你说它有效,但总是向右移动,即使向左滑动。我发现了你的问题,并回答了你为什么会有这个bug以及如何修复它。但现在你们编辑了它,提出了完全不同的问题,显然是完全不同的问题。我建议回复帖子,就像以前一样,接受答案关闭帖子,就像你在这个网站上应该做的那样。并为您的新问题创建新帖子。我希望你能理解。
desiredPos = transform.position + Vector3.left * 1.52f;
desiredPos = transform.position - Vector3.right * 1.52f;