【问题标题】:Enable character dash if a UI button is pressed如果按下 UI 按钮,则启用字符短划线
【发布时间】:2018-09-03 14:12:48
【问题描述】:

我目前正在尝试制作一款我的角色可以冲刺的手机游戏。我所有的角色动作都是通过 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
}

谢谢!

【问题讨论】:

  • 按钮激活时调整movespeedmovespeedX 值,不再激活时重置为默认值

标签: unity3d


【解决方案1】:

无论其他力如何,您都需要创建运动,这是一个示例。

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;
}

这是实现它的一种简单易行的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2021-11-14
    • 2016-03-13
    • 2015-10-29
    相关资源
    最近更新 更多