弹跳Unity 3D时将球向前移动

时间:2018-12-22 13:26:15

标签: c# unity3d

我有一个连续跳动的球,我将脚本放到下面,所以当玩家触摸屏幕时,球会向前移动。问题是当我触摸屏幕时,球停止弹跳,并且没有向前移动!

球形脚本:

public float ForwardSpeed;
public Rigidbody Ball;
public float Speed;
public static float GlobalGravity = -9.8f;
public float GravityScale = 1.0f;
bool isforce = false;

private void Start(){

    Ball = GetComponent<Rigidbody>();
    Ball.useGravity = false;

}


private void FixedUpdate(){

    if (Input.GetMouseButton(0)){

        Ball.velocity = transform.forward * ForwardSpeed * Time.deltaTime;
    }

    Vector3 gravity = GlobalGravity * GravityScale * Vector3.up;
    Ball.AddForce(gravity, ForceMode.Acceleration);


}

void force(){

    isforce = false;
    Ball.AddForce(Vector3.up * Speed, ForceMode.Impulse);
}

private void Update(){

    if (isforce == true){

        force();
    }
}

private void OnCollisionEnter(Collision collision){

    isforce = true;
}

0 个答案:

没有答案