我目前正在尝试制作一款手机游戏,我的角色可以冲刺。我所有的角色移动都通过UI按钮进行操作。我有带有方向功能的“向左移动”按钮和“向右移动”按钮。这是我的问题,如何使仪表板运动类似于空心骑士仪表板。如果我按下“破折号” UI按钮,我希望角色破折号。这是我的角色移动代码:
Rigidbody2D rb;
float playerspeed;
float movespeed;
float movespeedX;
private void Start()
{
rb = GetComponent <Rigidbody2D> ();
}
private void FixedUpdate()
{
Moveplayer(movespeed);
}
public void Moveplayer(float playerspeed)
{
myrb.velocity = new Vector2( movespeed, myrb.velocity.y);
if (playerspeed < 0 || playerspeed > 0 )
{
myanim.SetInteger ("State", 2);
}
}
public void Left()
{
movespeed = -movespeedX;
}
public void right()
{
movespeed = movespeedX;
}
public void StopMoving()
{
movespeed = 0;
}
public void Dash()
{
//execute dash when UI button is pressed
}
谢谢!
答案 0 :(得分:0)
您需要不考虑其他作用力来创建运动,这是一个示例。
private bool enableMovement = false;
private float dashingTime = 1f;
private float dashingStrength = 2f;
private void FixedUpdate()
{
if (enableMovement)
Moveplayer(movespeed);
}
public Dash()
{
enableMovement = false;
StartCoroutine(Dashing());
}
private IEnumerator Dashing()
{
myrb.velocity = new Vector2(movespeed * dashingStrength, myrb.velocity.y);
yield return new WaitForSeconds(dashingTime);
enableMovement = true;
}
这是一种简单易行的方法。