コード例 #1
0
ファイル: CameraMain.cs プロジェクト: imengyu/TestBuliding
    public void Shoot(bool center, int touchIndex)
    {
        ShowText("Shoot !");

        //获取鼠标点击位置
        //创建射线;从摄像机发射一条经过鼠标当前位置的射线

        Vector3 mousePos = Input.mousePosition;

        if (center)
        {
            mousePos = new Vector3(Screen.width / 2, Screen.height / 2, 0);
        }
        else if (touchIndex >= 0)
        {
            Touch touch = Input.GetTouch(touchIndex);
            mousePos = new Vector3(touch.position.x, touch.position.y, 0);
        }

        Ray ray = mainCamera.ScreenPointToRay(mousePos);

        //从摄像机的位置创建一个带有刚体的球ballPrefab为预制小球
        GameObject go          = null;
        BulletType currentType = bulletTypes[choosedBulletType];

        go = Instantiate(currentType.prefab, transform.position, Quaternion.identity);
        go.transform.rotation = Random.rotation;

        if (currentType.initCallback != null)
        {
            currentType.initCallback(go);
        }
        go.transform.SetParent(ballHost.transform);

        //发射数来的球沿着摄像机到鼠标点击的方向进行移动
        Rigidbody ball = go.GetComponent <Rigidbody>() as Rigidbody;

        ball.AddForce(shootForce * ray.direction);
    }