- Home /
Unet/Mirror spawning object on local crosshair position
Player characters have NetworkIdentity and Local Player Authority Check as well as NetworkTransform.
Qube have NetworkIdentify with nothing Check and NetworkTransform.
Both Host and Client can see Qubes on the same position.
Only Host can Spawn Qube on its crosshair position, the Client keep spawning the Qube on the wrong position.
I want to spawn the Qube on the position of local crosshair, the crosshair position is relative to player position and just circle around the player based on cursor position + vector2(0.2f, 0.2f) away from the player. It works fine on the host client but on the client that connects to the server, it spawns the Qube on the same position over and over again +(0.2, 0.2, 0) from player spawn position, completly ignoring the crosshair position of the Host or Client. I tried changing checking and unchecking many prosperities on NetworkIdentiy/NetworkManager/NetworkTransform on Players/Qubes but it didn't help much.
Finally out of desperation i tried to spawn crosshairs(even though i didn't want them to be visible to another player) by adding the prefab to SpawnAbleObject in NetworkManager and adding to the prefab the NetworkTransform&NetworkIdentiy, and there i saw it that on Host, the Clients crosshair position doesn't change at all and just sits on the previously mentioned +(0.2. 0.2, 0) away from the Client Spawn position, and that's where the Clients Qube gets spawned all the time. And on Client i can see the correct, constanlty changing Hosts crosshairs position(at least when i look at the Client instance running on the background while moving the mouse on Host instance, since when i move the mouse on Client Instance the Host crosshair slightly shifts when client crosshair moves, propably due to the fact that i run both of these instances on one computer and they both get the input from the mouse cursor position).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class PlayerMovement : NetworkBehaviour
{
public GameObject CrosshairPrefab;
GameObject Crosshair;
GameObject QubePrefab;
Vector2 PlayerPosition;
Vector2 CursorPosition;
Vector2 direction;
Vector2 crosshairSpawnPoint = new Vector2(0.2f, 0.2f);
public override void OnStartClient()
{
Cursor.visible = true;
Vector2 crossHairStartPos = transform.position + (Vector3)crosshairSpawnPoint;
Crosshair = Instantiate(CrosshairPrefab, crossHairStartPos, Quaternion.identity);
Crosshair.transform.SetParent(transform.parent);
}
[Command]
void CmdSpawnQube()
{
GameObject Qube = Instantiate(QubePrefab, Crosshair.transform.position, Quaternion.identity);
NetworkServer.Spawn(Qube);
}
void Update()
{
if (isLocalPlayer)
{
PlayerPosition = transform.position;
CursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = CursorPosition - PlayerPosition;
Vector2 spawnDistance = direction.normalized * 0.2f;
Crosshair.transform.position = PlayerPosition + Vector2.Scale(crosshairSpawnPoint, direction.normalized) + spawnDistance;
if (Input.GetKeyUp(KeyCode.Mouse0))
{
CmdSpawnQube();
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612185227im_/https://answers.unity.com/themes/thub/images/avi.jpg)