为什么当我按下LMB时球会掉下来?

时间:2019-05-23 17:21:37

标签: c# unity3d unity3d-editor

当我按下鼠标左键但我的球掉下来时,我试图使“第一人称控制器”炸毁(飞起来)

这用于Unity 4.5.5(我的笔记本电脑无法运行Unity的5.x.x版本)

using UnityEngine;
using System.Collections;

public class eeeboi : MonoBehaviour {
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;
    public float radius;
    public float force;

void Blow () {
    Collider[] col = Physics.OverlapSphere (transform.position, radius);
    foreach (Collider c in col) {
        if (c.name != "Plane"){
            c.GetComponent<Rigidbody>().AddExplosionForce (force, transform.position, radius);
        }
    }
}

void Update() {
    CharacterController controller = GetComponent<CharacterController> ();

    if (controller.isGrounded) {
        moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
        moveDirection = transform.TransformDirection (moveDirection);
        moveDirection *= speed;

        if (Input.GetButton ("Jump"))
            moveDirection.y = jumpSpeed;
    }

    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move (moveDirection * Time.deltaTime);

    if (Input.GetMouseButtonDown (0)) {
        Blow ();
        }
    }
}

我希望当我的球在下面时,“第一人称控制者”会飞起来,但是我的球掉下来却什么也没动

1 个答案:

答案 0 :(得分:1)

我相信您的AddExplosionForce方法需要第四个参数为非零值,例如3.0F,如以下example所示:AddExplosionForce(power, explosionPos, radius, 3.0F);

此外,请尝试使用Debug.Log method进行调试。希望对您有帮助!