Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Badgerfish · Sep 12, 2016 at 01:51 AM · networkingraycastclient-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 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; }

Comment
Add comment
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.

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

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


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