【问题标题】:Unity Cant jump and move on the same timeUnity不能同时跳跃和移动
【发布时间】:2014-11-11 09:37:24
【问题描述】:

您好,感谢您阅读本文。

我在 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));
    }
}

我怎样才能让我可以同时跳跃和移动。?

【问题讨论】:

  • 我不是 Unity 开发人员,所以只是一个问题:您确定您使用的设备同时接受多个触摸输入吗?
  • 我在 3 台设备上试用过,它们都应该能够接受多点触控输入。
  • 因为你的移动将速度设置为new Vector2,它会控制跳跃增加的力量。尝试为移动添加一个力,而不是一个全新的速度。
  • @Catwood 有没有机会帮忙提供一点代码?
  • 抱歉,更详细地查看您的代码,我认为这不是问题,因为您在创建新速度时使用了rigidbody2D.velocity.y。它一定是别的东西。

标签: c# unity3d


【解决方案1】:

从向上箭头代码:

(Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))

您正在检查第一次触摸是否刚刚开始,如果您按住移动箭头并点击向上跳跃,则“跳跃触摸”不是第一次触摸并且第一次触摸(用于移动)是不处于开始阶段。

点击跳跃然后移动的原因是因为在这种情况下第一次触摸是跳跃触摸(巧合而不是代码)。

您不想在此处检查第一次触摸,而是要检查向上箭头上方的触摸。

(不确定您实际上是如何做到的,但在获得 50 个代表之前我无法发表评论:()

【讨论】:

    【解决方案2】:

    我也是一个新手,几个小时前就遇到了这个问题,但我按照视频教程中的指示修复了它,所以修复方法是:向播放器添加一些拖动,在检查器中有一个“线性刚体组件中的“拖动”选项将其增加 0.3(0 是默认值)这确实解决了我的问题我希望它也能帮助你(我知道它真的很晚,但我只是在谷歌搜索我的问题时发现了你的问题)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多