Raycast doesnt work for client in netcode
Hello, I am currently trying to create an online game. I created a script that spawns one object per player when a player spawns. At first I just tried to change the position by using a random vector3 value as the position of the object.
//Move is called when button is pressed
Move(){
if (NetworkManager.Singleton.IsServer)
{
...
}
else
{
SubmitPositionRequestServerRpc();
}
}
void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
{
Object = Instantiate(test, transform.position, transform.rotation);
Object.GetComponent<NetworkObject>().Spawn();
changePosition(Object);
}
public void changePosition(GameObject Object)
{
Object.transform.position = GetRandomPositionOnPlane();
}
static Vector3 GetRandomPositionOnPlane()
{
return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
}
It worked for the client and also for the host. After that, I wanted Object to be permanently where the mouse is. For this I called the changePosition() function in the update function so that the object permanently changes its position. That also worked for the host and the client. But when I added this code to the changePosition() function:
if (mainCamera == null)
{
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue/*, layerMask*/))
{
/*Position.Value*/
Object.transform.position = raycastHit.transform.position;
}
it only worked on the host. This code is supposed to change the object's position to the mouse position. The object has gone to the host's mouse position and when a client is connected, the object has also gone from the client to the host's mouse position. But I want the object from the client to only go where the "mouse" from the client is.
Since I've been struggling with this problem for a few days and couldn't find a solution, I hope someone here can help me.
Your answer
Follow this Question
Related Questions
How can I make a simple Lobby scene, while staying in the same scene all the time? 0 Answers
Client's object spawn position and rotation network bug? 0 Answers
Networking - Host can spawn objects but client cant 0 Answers
How to correctly change scene with a Lobby Manager? 0 Answers
Looking for tips on identifying reason behind random client disconnects 0 Answers