【发布时间】: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。它一定是别的东西。