My Units wont move when produced

I have a problem that my units just spawn at the spawn point and spawn at each other, won’t move. I did everything like in the course, the code is the same, but for me, it’s not working.
Please help.
Thanks in advance

Hi there,
Is your specifically pertaining to that initial movement instructed by the unit spawner? Or will they not move at all, even when you select them and try to move?

First thing to check is that you have baked the navmesh. It’s also important to make sure they are being instructed to move to a valid position on the navmesh. Try adding some debug logs to your code to see if you can confirm that.

Let me know if you need further help!

Units are spawned at same position, on top of each other. When selected they move, but they are just spawned at same position, wont move on their own when spawned (spawn offset movement)

Can you please share your code for that initial movement? It could be it’s telling them to move to a position within their stopping distance. You might have to play with some values there.

using Mirror;

using TMPro;

using UnityEngine;

using UnityEngine.EventSystems;

using UnityEngine.UI;

public class UnitSpawner : NetworkBehaviour, IPointerClickHandler

{

[SerializeField] private Health health = null;

[SerializeField] private Unit unitPrefab = null;

[SerializeField] private Transform unitSpawnPoint = null;

[SerializeField] private TMP_Text remainingToReproduce = null;

[SerializeField] private Image productionProgressImage = null;

[SerializeField] private int maxUnitInQue = 5;

[SerializeField] private float spawnMoveRange = 5f;

[SerializeField] private float unitProduceDuration = 5f;

[SyncVar(hook = nameof(ClientHandleQueuedUnitsUpdated))]

private int unitsInProduction;

[SyncVar]

private float unitTimer;

private float progresImageVelocity;

private void Update()

{

if(isServer)

{

  ProduceUnits();

}

if(isClient)

{

  UpdateTimerDisplay();

}

}

#region  Server

public override void OnStartServer()

{

  health.ServerOnDie += HandleServerOnDie;

}

public override void OnStopServer()

{

  health.ServerOnDie -= HandleServerOnDie;

}

[Server]

private void ProduceUnits()

{

  if(unitsInProduction == 0){return; }

  unitTimer += Time.deltaTime;

  if(unitTimer < unitProduceDuration){return; }

 

    Vector3 spawnOffset = Random.insideUnitSphere * spawnMoveRange;

    spawnOffset.y = unitSpawnPoint.position.y;

   GameObject unitInstance = Instantiate(unitPrefab.gameObject,unitSpawnPoint.position , unitSpawnPoint.rotation);

    NetworkServer.Spawn(unitInstance, connectionToClient);

    UnitMovement unitMovement = unitInstance.GetComponent<UnitMovement>();

    unitMovement.ServerMove(unitSpawnPoint.position + spawnOffset);

    unitsInProduction--;

    unitTimer = 0f;

}



[Server]

private void HandleServerOnDie()

{

    NetworkServer.Destroy(gameObject);

}

[Command]

private void CmdSpawnUnit()

{

   if(unitsInProduction == maxUnitInQue){return;}

   RTSPlayer player = connectionToClient.identity.GetComponent<RTSPlayer>();

   if(player.GetResources() < unitPrefab.GetResaurceCost()){return;}

   unitsInProduction++;

   

   player.SetResources(player.GetResources() - unitPrefab.GetResaurceCost());

}

#endregion

#region Client

private void UpdateTimerDisplay()

{

float newProgress = unitTimer / unitProduceDuration;

if(newProgress < productionProgressImage.fillAmount)

{

  productionProgressImage.fillAmount = newProgress;

}

else

{

 productionProgressImage.fillAmount = Mathf.SmoothDamp(productionProgressImage.fillAmount, newProgress, ref progresImageVelocity, 0.1f);

}

}

public void OnPointerClick(PointerEventData eventData)

{

  if(eventData.button != PointerEventData.InputButton.Left){return;}

  if(!hasAuthority){return;}

 

  CmdSpawnUnit();

}

private void ClientHandleQueuedUnitsUpdated(int oldUnits, int newUnits)

{

  remainingToReproduce.text = newUnits.ToString();

}

#endregion

}

What is your spawnMoveRange set to? Also on the unit you are spawning, what is the stopping distance of their navMeshAgent?

Also debug.log the spawnOffset so you can see what kind of points you are getting.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms