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); } } }
0
Best Answer
-
Hello @Kobaltic,
This happens because we don't follow the Unity's Update method execution, to guarantee determinism we have a fixed interval between each frame simulated which means we can eventually miss the frame where the input (mouse for example) was UP or DOWN. You have to use "Input.GetMouseButton" and track its previous state, so in the OnSyncedUpdate you can check if the input was down/up. To a better understanding you can look at our demos, there is a "SimpleControl.cs" script that implements what I mention above. Any doubts please reply us.5
Answers
-
Hello @Kobaltic,
This happens because we don't follow the Unity's Update method execution, to guarantee determinism we have a fixed interval between each frame simulated which means we can eventually miss the frame where the input (mouse for example) was UP or DOWN. You have to use "Input.GetMouseButton" and track its previous state, so in the OnSyncedUpdate you can check if the input was down/up. To a better understanding you can look at our demos, there is a "SimpleControl.cs" script that implements what I mention above. Any doubts please reply us.5 -
Thank you. I was able to get it working using that method.0