The sprite only shows below the unit on the host window. Both of the players’ sprites show in this window, but the client’s doesn’t disappear when you click off of it. Does anyone have an idea why? (The movement works perfectly though).
Thanks in advance.
Here is an image of the problem that I’m experiencing:
Here are my scripts:
Unit:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using UnityEngine.Events;
public class Unit : NetworkBehaviour
{
[SerializeField] private unitMovement movementScript;
[SerializeField] private UnityEvent onSelected;
[SerializeField] private UnityEvent onDeselected;
public unitMovement GetUnitMovement()
{
return movementScript;
}
[Client]
#region Client
public void Select()
{
if (!hasAuthority) return;
onSelected?.Invoke();
}
[Client]
public void Deselect()
{
if (!hasAuthority) return;
onDeselected?.Invoke();
}
#endregion
}
UnitSelectionHandler:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class UnitSelectionHandler : MonoBehaviour
{
[SerializeField] private LayerMask layerMask;
private Camera mainCamera;
public List<Unit> SelectedUnits { get; } = new List<Unit>();
private List<Unit> deSelectedUnits = new List<Unit>();
private void Start()
{
mainCamera = Camera.main;
}
private void Update()
{
if(Mouse.current.leftButton.wasPressedThisFrame)
{
StartSelectedUnits();
}
else if (Mouse.current.leftButton.wasReleasedThisFrame)
{
ClearSelectionArea();
}
}
private void StartSelectedUnits()
{
foreach (Unit thisUnit in SelectedUnits)
{
thisUnit.Deselect();
}
SelectedUnits.Clear();
}
private void ClearSelectionArea()
{
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask)) return;
/*
Trygetcomponent: Tries to get the "Unit" component. If it does, it will output a new unit (The unit that it hit).
If there is no "Unit" script on the hit, it will return.
*/
if (!hit.collider.TryGetComponent<Unit>(out Unit unit)) return;
if (!unit.hasAuthority) return;
SelectedUnits.Add(unit);
foreach(Unit selectedUnit in SelectedUnits)
{
selectedUnit.Select();
}
}
}
UnitCommandGiver:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class UnitCommandGiver : MonoBehaviour
{
[SerializeField] private UnitSelectionHandler unitSelectionHandler;
[SerializeField] private LayerMask layerMask = new LayerMask();
Camera mainCamera;
private void Start()
{
mainCamera = Camera.main;
}
private void Update()
{
if (!Mouse.current.rightButton.wasPressedThisFrame) return;
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask)) return;
TryMove(hit.point);
}
private void TryMove(Vector3 point)
{
foreach(Unit unit in unitSelectionHandler.SelectedUnits)
{
unit.GetUnitMovement().CmdMove(point);
}
}
}
unitMovement :
using UnityEngine;
using Mirror;
using UnityEngine.AI;
public class unitMovement : NetworkBehaviour
{
[SerializeField] private NavMeshAgent navMesh;
#region Server
//See "myNetworkPlayer" for explanation
[Command]
public void CmdMove(Vector3 position)
{
//NavMesh.SamplePosition: finds nearest point on navmesh, even if the position is off of the navmesh.
//Hit: The resulting location.
if (!NavMesh.SamplePosition(position, out NavMeshHit hit, 1f, NavMesh.AllAreas)) return;
else navMesh.SetDestination(hit.position);
}
#endregion
}