- Home /
Issuing orders to Units in Networked Turn-based game
I have the basics of a networked Turn-based strategy game in place.
Two players can connect, one is the Host, the other a Client. They can see things happening and have independent controls. All is working fine until I get to selecting and issuing orders to my units.
I can select units on the Host, and I click on the Move UI button, which sets the player mouse manager into 'movement mode' which is a simple way of saying it starts polling for a possible position to place the unit as you move the mouse around by using a simple raycast. When you Click while in 'movement mode' it a command is issued to the Game manager, which then sends calls the function 'MoveToPosition(location)' on the given unit.
The unit has this script attached.
public class TrooperMovement : NetworkBehaviour {
public Transform start;
public Vector3 end;
public float speed = 1f;
private float startTime;
private float journeyLength;
public bool moving = false;
public float moveLeft;
void Start(){
start = GetComponent<Trooper> ().transform;
startTime = Time.time;
moveLeft = GetComponent<Trooper> ().move1;
}
public void MoveToPosition(Vector3 location){
Debug.Log ("Moving to: " + location);
end = location;
moving = true;
}
void Update(){
if (transform.position != end && moving) {
//CheckRange ();
transform.position = Vector3.Lerp (start.position, end, speed * Time.deltaTime);
} else {
moving = false;
}
}
}
So here's my issue. It moves fine in the host and the client can see the result as the unit moves towards the location. But when I try it in the Client, I get an error saying that no object is selected.
Which is odd considering that the projectors letting the player know that the unit is 'selected' and in 'movement mode' appear just fine, but as soon as I click and issue the command, nothing happens.
Here is the code that checks the raycast for movement, you can ignore most of it up until 'Move plz', that's when the click happens in a valid location. void CheckMovement(){ //if movement range is within range then set the move location and set the unit do its thing Ray rayToWorld = myView.ScreenPointToRay (Input.mousePosition); RaycastHit hit; if (Physics.Raycast (rayToWorld, out hit, 100.0f, terrainLayer) && !EventSystem.current.IsPointerOverGameObject()) { GameObject hitObject = hit.transform.gameObject; if (hitObject.tag == "Terrain" && DistanceWithinMoveValue(Vector3.Distance(hit.point, selectedUnit.transform.position))) {
//here is a valid location to move to (hitting terrain)
if (debug) {
Debug.DrawRay (rayToWorld.origin, rayToWorld.direction * 100, Color.cyan);
}
//now check if unit is adjacent to a wall
if(UnitAdjacentToWall(hit)){
RaycastHit terrainHit;
if(Physics.Raycast (hit.point + (new Vector3(hit.normal.x,0,hit.normal.z) * clearanceRadius), Vector3.down, out terrainHit, 50f, terrainLayer)){
if(debug)Debug.DrawRay(hit.point + (new Vector3(hit.normal.x, 0, hit.normal.z) * clearanceRadius), Vector3.down * terrainHit.distance, Color.yellow);
proposedMoveLocation = terrainHit.point + new Vector3(terrainHit.normal.x, 0, terrainHit.normal.z) * clearanceRadius;
}
//FIXME There should be an extra check here to make sure the unit does not stand with its lip off the edge, so half-on a surface is not okay
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Move plz");
CmdSetMovementLocation (proposedMoveLocation);
}
//cannot place units on walls for now
//proposedMoveLocation = hit.point;
}else{
// this is valid open ground
proposedMoveLocation = hit.point;
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Move plz");
CmdSetMovementLocation (proposedMoveLocation);
}
}
} else {//hitting a unit
if (debug) {
Debug.DrawRay (rayToWorld.origin, rayToWorld.direction * 100, Color.red);
}
}
} else {//not hitting anything valid
if (debug) {
Debug.DrawRay (rayToWorld.origin, rayToWorld.direction * 100, Color.yellow);
}
}
}
Here is the command that goes up to the server to issue the movement order. I get 'K I'll Move' in the console even from the client, but the selectedTrooper is 'null' so it never moves.
Yet on the Host it works just fine. Can anyone point out where I can improve this code to make it work? [Command] void CmdSetMovementLocation(Vector3 location){ //pass off the location to the movement function in the trooper Debug.Log("K I'll Move"); if (selectedUnit != null) { GameManager.instance.GetComponent ().IssueMoveOrder (selectedUnit, location); //selectedUnit.GetComponent ().MoveToPosition (location); } inMovementMode = false; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612092631im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Make An Online Multiplayer Game With My Own Server? 2 Answers
Server unable to spawn prefab 0 Answers
Qos type ReliableFragmented channel, won't fragment our stream 2 Answers
Issue building Android build with com.unity.transport in it 0 Answers
Client side prediction, rigidbody.velocity, photon network 0 Answers