Unity - Character Motor Forward Dash问题

时间:2014-09-07 17:25:44

标签: unity3d unityscript

我正在尝试修改Unity的内置字符电机,以便在用户按下短划线按钮时让我的角色向前冲。这个破折号功能很简单,我只希望将角色快速朝向他们所朝向的方向,行程距离约为3米。这是我到目前为止的代码:

在update()函数内部,如果已按下短划线输入键,则调用此函数

private function ApplyInputDashForce (velocity : Vector3) {

    if (!inputDash || !canControl)
        dashing.lastButtonDownTime = -100;

    if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
        dashing.lastButtonDownTime = Time.time;

    if (dashing.enabled && canControl) // && (Time.time - dashing.lastButtonDownTime < 0.2) 
    {            
        dashing.dashing = true;
        dashing.lastButtonDownTime = -100;
        dashing.dashCounter ++;
        var cam : Transform = Camera.main.transform;
        dashing.dashDir = Vector3(cam.localPosition.x, 0, cam.localPosition.y); //(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));            
        velocity += dashing.dashDir * dashing.baseDistance;
        Debug.Log("We just dashed.");            
    }       

    return velocity;
}

我设法让破折号工作有点唯一的问题是它不会冲向正确的方向。但只穿过X轴。我需要能够向球员面对的方向冲刺。我也试过这个

dashing.dashDir = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));            
velocity += dashing.dashDir * dashing.baseDistance;

对于我做错的任何帮助都将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您的角色控制器是fps,那么不要只更改特定轴的值,更改CharacterMotor.maxForwardSpeed并在释放按钮时将其恢复。

短划线将面向角色。

gameObject.GetComponent(CharacterMotor).maxForwardSpeed =  gameObject.GetComponent(CharacterMotor).maxForwardSpeed * (((speed)/100.0)+1);

通过TPS或FPS控制器的字符电机。