我正在制作一款无尽的跑步者移动的游戏。PC控制工作完美,左右触摸/滑动控制也工作正常。问题是当我向上滑动跳跃时,游戏者跳跃,但随后也向左或向右移动(看起来是随机的)。就我的一生而言,我都想不通。这是我的第一个C#项目,所以我的代码/游戏是教程和谷歌搜索的弗兰肯斯坦。我发现刚体组件有点太晚了,但我不想现在就开始使用它,如果我没有必要...请帮助,我完全不知所措。我也想提前道歉,如果我的代码是非常混乱和不连贯的,我已经尽力了。以下是我的球员移动脚本:
PC质控品
public class PlayerMove : MonoBehaviour
{
public float moveSpeed = 8;
public float leftRightSpeed = 7;
static public bool canMove = false;
public KeyCode moveL;
public KeyCode moveR;
public float horizVel = 0;
public int laneNum = 2;
public string controlLocked = "n";
private float speedUpTime;
private float waitTime = 0.45f;
private Vector3 startTouchPosition;
private Vector3 endTouchPosition;
public GroundCheck groundCheck;
public float jumpForce=7;
public float gravity = -9.81f;
public float gravityScale = 3;
public float jumpCoolDown = 0.5f;
private float lastJumpTime = 0;
float velocity;
// Start is called before the first frame update
void Start()
{
speedUpTime = Time.time;
}
// Update is called once per frame
void Update()
{
if (Time.time - speedUpTime >= 10f)
{
moveSpeed += 1;
leftRightSpeed +=1;
waitTime -= 0.05f;
speedUpTime = Time.time;
}
transform.position += new Vector3(horizVel, 0, moveSpeed) * Time.deltaTime;
if((Input.GetKeyDown(moveL)) && (laneNum>1)&& (controlLocked == "n"))
{
horizVel = -leftRightSpeed;
StartCoroutine(stopSlide());
laneNum -= 1;
controlLocked = "y";
}
if((Input.GetKeyDown(moveR)) && (laneNum<3)&& (controlLocked == "n"))
{
horizVel = leftRightSpeed;
StartCoroutine(stopSlide());
laneNum += 1;
controlLocked = "y";
}
velocity += gravity * gravityScale * Time.deltaTime;
if (groundCheck.isGrounded && velocity < 0)
{
velocity = 0;
lastJumpTime = 0;
}
if (Input.GetKeyDown(KeyCode.Space) && (Time.time - lastJumpTime) > jumpCoolDown)
{
lastJumpTime = Time.time;
velocity = jumpForce;
}
transform.Translate(new Vector3(0, velocity, 0) * Time.deltaTime);
触摸控制
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
startTouchPosition = Input.GetTouch(0).position;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
endTouchPosition = Input.GetTouch(0).position;
// Check for horizontal swipe
if((endTouchPosition.x < startTouchPosition.x) && (laneNum>1)&& (controlLocked == "n"))
{
horizVel = -leftRightSpeed;
StartCoroutine(stopSlide());
laneNum -= 1;
controlLocked = "y";
}
if((endTouchPosition.x > startTouchPosition.x) && (laneNum < 3) && (controlLocked == "n"))
{
horizVel = leftRightSpeed;
StartCoroutine(stopSlide());
laneNum += 1;
controlLocked = "y";
}
// Check for vertical swipe
if ((endTouchPosition.y > startTouchPosition.y) && (Time.time - lastJumpTime) > jumpCoolDown)
{
lastJumpTime = Time.time;
velocity = jumpForce;
}
}
}
IEnumerator stopSlide()
{
yield return new WaitForSeconds(waitTime);
horizVel = 0;
controlLocked = "n";
}
}
我已经玩了好几个小时了,我甚至不记得我试过什么。要么玩家停止移动,要么右变成左,要么跳是右,要么左是跳,等等......
1条答案
按热度按时间x3naxklr1#
既然你说触摸控制是问题所在,那么这个答案将集中在你的第二块代码上。
原因似乎是当玩家向上滑动时,他们的手指几乎从来不会在同一个x位置。为了说明这一点,试着在屏幕上以精确的直线向上滑动。
计算机对事物的理解非常精确,所以即使偏差0.0000001也足以被算作更少/更多。
那么这意味着什么呢?当你向上滑动时,你的手指x***和***y的位置将会改变--导致两个if语句都被激活
一个不会太耗费时间的解决方案是为刷卡创建一个"死区"。
简单地说,死区是除了动作之外还必须通过的阈值。
以下是死区的示例: