• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Badgerfish · Sep 12, 2016 at 01:51 AM · raycastnetworkingclient-serverturn-based

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 t$$anonymous$$ngs 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, w$$anonymous$$ch sets the player mouse manager into 'movement mode' w$$anonymous$$ch 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 w$$anonymous$$le in 'movement mode' it a command is issued to the Game manager, w$$anonymous$$ch then sends calls the function 'MoveToPosition(location)' on the given unit.

The unit has t$$anonymous$$s 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.

W$$anonymous$$ch 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, not$$anonymous$$ng 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 wit$$anonymous$$n range then set the move location and set the unit do its t$$anonymous$$ng Ray rayToWorld = myView.ScreenPointToRay (Input.mousePosition); RaycastHit $$anonymous$$t; if (Physics.Raycast (rayToWorld, out $$anonymous$$t, 100.0f, terrainLayer) && !EventSystem.current.IsPointerOverGameObject()) { GameObject $$anonymous$$tObject = $$anonymous$$t.transform.gameObject; if ($$anonymous$$tObject.tag == "Terrain" && DistanceWit$$anonymous$$nMoveValue(Vector3.Distance($$anonymous$$t.point, selectedUnit.transform.position))) {

                 //here is a valid location to move to ($$anonymous$$tting terrain)
                 if (debug) {
                     Debug.DrawRay (rayToWorld.origin, rayToWorld.direction * 100, Color.cyan);
                 }
                 //now check if unit is adjacent to a wall
                 if(UnitAdjacentToWall($$anonymous$$t)){
                     RaycastHit terrainHit;
                     if(Physics.Raycast ($$anonymous$$t.point + (new Vector3($$anonymous$$t.normal.x,0,$$anonymous$$t.normal.z) * clearanceRadius), Vector3.down, out terrainHit, 50f, terrainLayer)){
                         if(debug)Debug.DrawRay($$anonymous$$t.point + (new Vector3($$anonymous$$t.normal.x, 0, $$anonymous$$t.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 = $$anonymous$$t.point;
                 }else{
                     // t$$anonymous$$s is valid open ground
                     proposedMoveLocation = $$anonymous$$t.point;
                     if (Input.GetMouseButtonDown (0)) {
                         Debug.Log ("Move plz");
                         CmdSetMovementLocation (proposedMoveLocation);
                     }
                 }
                     
             } else {//$$anonymous$$tting a unit
                 if (debug) {
                     Debug.DrawRay (rayToWorld.origin, rayToWorld.direction * 100, Color.red);
                 }
             }
         } else {//not $$anonymous$$tting anyt$$anonymous$$ng 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 t$$anonymous$$s 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; }

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Send a file using NetworkServer class. 0 Answers

How to watch in client applications what it is happening in the server one? 0 Answers

UNET Different size of spawned object on host and client 1 Answer

Need help understanding ClientRpc from Unity Networking tutorial,Question about clientrpc 1 Answer

Re-send ClientRPC after new player joins 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges