Network ray returns different mouse positions
Hello, I am creating a RTS in unity. I am new to the whole networking thing and followed some tutorials to get it all set up. I am having a weird issue. I have 2 gameobjects that are controlled by a player. Here are the 2 scripts that use Input.mousePosition.
Here is the PlayerController script. Input.mousePosition returns the correct value on both client and host when I click.
void Start(){
player = GetComponent<NavMeshAgent> ();
}
void FixedUpdate () {
if (!isLocalPlayer) {
return;
}
Click ();
}
void Click(){
if (Input.GetMouseButtonDown (1)) {
Debug.Log (transform.name + "has clicked " + Input.mousePosition);
Ray ray = GetComponentInChildren<Camera>().ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 100f, canMove.value)) {
Debug.Log ("position is " + hit.point);
position = hit.point;
player.destination = position;
}
}
}
}
Here is the other script that returns the same value as playercontroller when i click on the host. But when i click on the client, its a different value.
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
CmdClick ();
}
}
[Command]
void CmdClick(){
Ray ray = GetComponentInChildren<Camera>().ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
Debug.Log (transform.name + "has clicked " + Input.mousePosition);
if (Physics.Raycast (ray, out hit, 100f, BuildBlock.value)) {
bool canBuild = hit.transform.GetComponent<CanBuild>().canBuild;
if (canBuild) {
GameObject g = (GameObject)Instantiate (towerone);
g.transform.position = hit.transform.position + towerone.transform.position;
hit.transform.GetComponent<CanBuild> ().canBuild = false;
NetworkServer.Spawn (g);
Debug.Log (" Has Built");
} else {
Debug.Log ("Cant Build There");
}
}
}
This boggles my mind. Each client has their own camera attached to their gameobject.
edit: I realized that when i hit space, both client and host base the input.mouseposition based on the host client. I believe the issue is with my [Command] statement but i am not quite sure how to proceed.
Answer by carbonroast · Oct 04, 2017 at 10:36 PM
If anyone runs into this issue, I solved it by removing the raycast from the Command function. I made a manager dictionary(string,Gameobject) that kept track of all my blocks. My raycast finds the name of the block it hits, sends the string to the manager which gets the gameobject and then instantiate the network spawned object using that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with selecting objects through raycast in multiplayer game 0 Answers
Raycast Hit on a Server Object? 0 Answers
How can you spawn objects on network using raycast? 0 Answers
Raycast doesnt work for client in netcode 0 Answers
cannot reference parent network identity.netId when applying damage 1 Answer