I am having trouble getting the transformOfHit variable in this script to sync with other clients. Basically what I can see is happening is that the transformOfHit variable is reliant on the Ray “hit”. Hit is set equal to Input.mousePosition so i’m pretty sure that the client that has "MyView set to true is using its own mouse position instead it thinks that I am not clicking on anything. If I am wrong please correct me but if not a solution to get a reference to a clients mouse position would be greatly appreciated as I have spent more than 10 hours over 2 days just trying to find a way to get the reference to this so I know what the client has clicked and thus set to its target. I am using this to fight the same enemy which is a scene object as other clients so they can both take its health down and kill it.
I also think it might be worth mentioning that I have 2 cameras on the prefab I am instantiating for each player. If the PhotonView IsMine then I am activating the main camera and if not then I am activating the secondary camera so I can still have a camera that is capable of raycasting based on the mouse position on the client.
public class Raycaster : MonoBehaviourPunCallbacks
{
public PhotonView PV;
private Transform camRotate;
float currentMousePosition;
float pastMousePosition;
float screenWidth;
float screenHeight;
public Camera cam;
public Player playerScript;
public Transform transformOfHit;
public RaycastHit hit;
// Start is called before the first frame update
void Start()
{
PV = GetComponent<PhotonView>();
playerScript = GetComponent<Player>();
screenWidth = Screen.width;
screenHeight = Screen.height;
}
// Update is called once per frame
void Update()
{
if (!PV.IsMine)
{
if (cam == null)
{
cam = GetComponentInChildren<Camera>();
}
return;
}
if (cam == null)
{
cam = GetComponentInChildren<Camera>();
}
if (Input.GetMouseButtonDown(0))
{
// if left button pressed...
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Debug.Log("This ray hit assigned in this raycaster");
PV.RPC("RpcSetHit", RpcTarget.OthersBuffered);
if (Physics.Raycast(ray, out hit))
{
//Set transforOfHit to whatever the transform of the object that was hit
PV.RPC("RpcSetTransformOfHit", RpcTarget.AllBuffered);
// the object identified by hit.transform was clicked
// do whatever you want
if (hit.transform.tag == "NPC")
{
print("You clicked an NPC");
//If there is already one created then dont create one
if (GameObject.Find("ConvoBox(Clone)") == false)
{
//Flips the convobox after instantiation
Quaternion hitQuaternion = hit.transform.rotation * Quaternion.Euler(0, 180f, 0);
//Create the prefab
Instantiate(Resources.Load("ConvoBox"), new Vector3(hit.transform.position.x,
hit.transform.position.y + 5, hit.transform.position.z + 5), hitQuaternion);
}
else
Destroy(GameObject.Find("ConvoBox(Clone)"));
}
if (hit.transform.tag == "Teleporter")
{
print("You clicked a Teleporter");
if (hit.transform.name == "Flower Scene Teleporter")
{
//If there is already one created then dont create one
if (GameObject.Find("FlowerSceneTeleporterConvo(Clone)") == false)
{
Quaternion hitQuaternion = transform.rotation;
//Create the prefab
Instantiate(Resources.Load("FlowerSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
//Create the Prefab
//Instantiate(Resources.Load(""), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
}
}
if (hit.transform.name == "Hell Scene Teleporter")
{
//If there is already one created then dont create one
if (GameObject.Find("HellSceneTeleporterConvo(Clone)") == false)
{
Quaternion hitQuaternion = transform.rotation;
//Create the prefab
Instantiate(Resources.Load("HellSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
hit.transform.position.y + 20, hit.transform.position.z + 20), hitQuaternion);
//Create the Prefab
//Instantiate(Resources.Load("HellSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
}
}
if (hit.transform.name == "Winter Scene Teleporter")
{
//If there is already one created then dont create one
if (GameObject.Find("WinterSceneTeleporterConvo(Clone)") == false)
{
Quaternion hitQuaternion = transform.rotation;
//Create the prefab
Instantiate(Resources.Load("WinterSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
//Create the Prefab
//Instantiate(Resources.Load("WinterSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
}
}
if (hit.transform.name == "Test Scene Teleporter")
{
//If there is already one created then dont create one
if (GameObject.Find("TeleporterConvo(Clone)") == false)
{
Quaternion hitQuaternion = transform.rotation;
//Create the prefab
Instantiate(Resources.Load("TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
//Create the Prefab
//Instantiate(Resources.Load("TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
}
}
if (hit.transform.name == "Floor 1 Teleporter")
{
//If there is already one created then dont create one
if (GameObject.Find("Floor1TeleporterConvo(Clone)") == false)
{
Quaternion hitQuaternion = transform.rotation;
//Create the prefab
Instantiate(Resources.Load("Floor1TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
//Create the Prefab
//Instantiate(Resources.Load("Floor1TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
}
}
}
if (hit.transform.tag == "Mob")
{
PV.RPC("RpcSetTargetMob", RpcTarget.AllBuffered);
print("You clicked on " + transformOfHit.name);
}
}
}
}
[PunRPC]
void RpcSetTargetMob()
{
playerScript.targetMob = transformOfHit;
}
[PunRPC]
void RpcSetTransformOfHit()
{
transformOfHit = hit.transform;
}
[PunRPC]
void RpcSetHit()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Debug.Log("This ray hit assigned in some1 elses raycaster");
}
}