Issue with mouse button up not always working

I am having issues with the mouse button up not always working. I set up my game so when the user clicks on the screen in will spawn a character. This happens about 50% of the time. The other 50% nothing happens. Not sure why this is happening.


using UnityEngine;
using System.Collections;
using TrueSync;

public class PlayerSpawn : TrueSyncBehaviour
{
    public GameObject playerPrefab;
    private TSVector playerPos;
    private Camera currentCamera;
    private Vector3 mousePos;
    private Vector3 newMousePos;
    byte MBDbyte;
    bool mouseButtonDown;    

    public override void OnSyncedStart()
    {
      base.OnSyncedStart();
      currentCamera = Camera.main;     
    }

    public override void OnSyncedInput()
    {
        MBDbyte = 0;
        mouseButtonDown = Input.GetMouseButtonUp(0);
        if (mouseButtonDown)
        {
            MBDbyte = 1;
            mousePos = Input.mousePosition;
            mousePos.z = 10;
            newMousePos = currentCamera.ScreenToWorldPoint(mousePos);
            playerPos = new TSVector(newMousePos.x, newMousePos.y,newMousePos.z);
            TrueSyncInput.SetTSVector(1, playerPos);
            TrueSyncInput.SetByte(0, MBDbyte);
        } 
    }

    public override void OnSyncedUpdate()
    {        
        byte spawn = TrueSyncInput.GetByte(0);
        TSVector spawnPos = TrueSyncInput.GetTSVector(1);
        if (spawn == 1)
        {
           TrueSyncManager.SyncedInstantiate(playerPrefab, spawnPos, TSQuaternion.identity);
        }       
    }
}

Best Answer

Answers

  • Thank you. I was able to get it working using that method.