Instantiate Objects by pressing the trigger button of controller

Hello guys,

I am trying to instantiate Objects by pressing the trigger Button of the controller. But nothing seems to work. I tried with RPC and PhotonNetwork.Instantiate. With RPC I am not sure how to implement when the trigger gets pressed.

Below both scripts:
void Update()
    {
        print("start SpawnCubes");

        PhotonView photonView = this.GetComponent<PhotonView>();
        photonView.RPC("SpawnCube", PhotonTargets.AllBuffered, transform.position, transform.rotation, 0);
    }

    public Transform CubePrefab;

    [PunRPC]
    void SpawnCube(Vector3 pos, Vector3 rot)
    {
        gameObject.transform.position = pos;
        gameObject.transform.rotation = Quaternion.Euler(rot);
    }
and with PhotonNetwork.Instantiate:
public class SpawnCubes : Photon.MonoBehaviour {

    private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
    public bool triggerButtonDown = false;
    public bool triggerButtonUp = false;
    public bool triggerButtonPressed = false;

    private SteamVR_Controller.Device controller { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
    private SteamVR_TrackedObject trackedObj;


    public GameObject CubePrefab;

    void Update() {

        print("spawn script start");

            var lDevice = SteamVR_Controller.Input((int)ViveManager.Instance.leftHand.GetComponent<SteamVR_TrackedObject>().index);
            var rDevice = SteamVR_Controller.Input((int)ViveManager.Instance.rightHand.GetComponent<SteamVR_TrackedObject>().index);

            if (lDevice.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
            {
                PhotonNetwork.Instantiate("CubePrefab", ViveManager.Instance.leftHand.transform.position, ViveManager.Instance.leftHand.transform.rotation, 0);
            }

            if (rDevice.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
            {
                PhotonNetwork.Instantiate(CubePrefab.name, ViveManager.Instance.rightHand.transform.position, ViveManager.Instance.rightHand.transform.rotation, 0);
            }

        else
        {
            print("instantiating cube did not work");
            return;
        }
    }
Thanks for any help =)

Comments

  • Hi @deadlysnix,

    the problem with your RPC call is, that you actually don't have any Instantiation call in the function. Please note that instantiating objects each time the Update function is called, is very inefficient and may result in game crashes.

    For the controller example: make sure that your input is actually registered. You can easily do this by adding Debug.Log(...) calls inside the Input condition. If those logs are printed to the console, your input is properly recognized. Inside this condition you can either use PhotonNetwork.Instantiate(...) or use the RPC function of the attached PhotonView in order to instantiate a certain object. If you use the RPC approach, please take a look at the Manual Instantiation section of this documentation page (at the bottom of the page) to properly instantiate objects across the network.