您好,感谢您阅读本文。
我在 Unity 中制作了一个小游戏,我终于可以使用触摸输入进行移动控制了。
但是现在我在结合移动和跳跃部分时遇到了一些问题。如果我在移动,我不能跳,但如果我在跳,我可以移动。
我的每个箭头键都包含一个脚本,然后调用“RobotController”脚本开始移动。
ArrowRight 和 ArrowLeft 脚本。它们看起来非常相似,所以我只发布 1:
private RobotController PlayermoveRight;
// Use this for initialization
void Start () {
PlayermoveRight = GameObject.Find("Player").GetComponent<RobotController>();
}
void OnMouseOver()
{
if(Input.touchCount >= 1)
{
var touchr = Input.touches[0];
if(touchr.phase != TouchPhase.Ended && touchr.phase != TouchPhase.Canceled)
{
PlayermoveRight.MoveRight();
}
else
{
}
}
}
ArrowUp 脚本:
void OnMouseOver()
{
GameObject Go = GameObject.Find("Player");
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
{
Go.GetComponent<RobotController>().Jump();
}
}
和 RobotController 脚本:
public double moveTime = 0.1;
public double moveTimeR = 0.1;
private double lastPressedTime = 0.0;
private double PressRight = 0.0;
public void MoveLeft() // If ArrowLeft is clicked or Pressed
{
lastPressedTime = Time.timeSinceLevelLoad;
}
public void MoveRight() // If ArrowRight is clicked or Pressed
{
PressRight = Time.timeSinceLevelLoad;
}
void FixedUpdate () {
if (PressRight + moveTimeR > Time.timeSinceLevelLoad)
{
rigidbody2D.velocity = new Vector2 (maxSpeed, rigidbody2D.velocity.y);
}
else if (lastPressedTime + moveTime > Time.timeSinceLevelLoad)
{
rigidbody2D.velocity = new Vector2 (maxSpeed - maxSpeed - maxSpeed, rigidbody2D.velocity.y);
}
else
{
rigidbody2D.velocity = new Vector2(0.0f, rigidbody2D.velocity.y);
}
}
public void Jump()
{
if (isOnGround == true) {
anim.SetBool("Ground",false);
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
}
我该怎么做才能同时跳跃和移动。?
最佳答案
从向上箭头代码:
(Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))
您正在检查第一次触摸是否刚刚开始,如果您按住移动箭头并点击以跳跃“跳跃触摸”不是第一次触摸并且第一次触摸(对于移动)是尚未进入开始阶段。
它在击中跳跃然后移动时起作用的原因是因为在这种情况下第一次触摸是跳跃触摸(巧合而不是代码)。
你不想检查这里的第一次触摸,你想检查向上箭头上方的触摸。
(不确定您实际上是怎么做到的,但在获得 50 个代表之前我无法发表评论 :( )
关于c# - Unity不能同时跳跃和移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26861726/