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 iveL_newO · May 15, 2018 at 06:10 AM · networkingclientcommands

[Command] not being called by clients

Here is my complete code, if you don't want to read it all I the more important parts are at the bottom. using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine.Networking;

 [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
 public class playerController : NetworkBehaviour
 {
     //this isn't mine (Don't edit)
     Vector2 _mouseAbsolute;
     Vector2 _smoothMouse;
     public Vector2 clampInDegrees = new Vector2(360, 180);
     public bool lockCursor;
     public Vector2 sensitivity = new Vector2(2, 2);
     public Vector2 smoothing = new Vector2(3, 3);
     public Vector2 targetDirection;
     //from here on is mine
 
     public Transform player;
     public int cubes = 0;
     public int textNum = 10;
     public float speed = 15;
     public float shootSpeed = 10;
     public float timer = 0;
     public UnityEngine.Transform sameBlock = null;
     public bool playing;
     public bool blueTrue;
     public bool dontGo;
     public UnityEngine.UI.Text text;
     public Material red;
     public GameObject playButton;
     public GameObject blueButton;
     public GameObject redButton;
     public GameObject canvas;
     public Material redBox;
     public Material blueBox;
     Camera cam;
 
     Object prefab;
     Object canvasPrefab;
 
 
     void Start(){
 
         playing = false;
         player = this.transform;
 
         canvasPrefab = Resources.Load ("Canvas");
         canvas = Instantiate (canvasPrefab) as GameObject;
 
         if (isLocalPlayer) {
             canvas.transform.SetParent (player);
         } else {
             Destroy (canvas);
         }
 
         playButton = canvas.transform.GetChild (2).gameObject;
         playButton.GetComponent<Button> ().onClick.AddListener (play);
         blueButton = canvas.transform.GetChild (4).gameObject;
         blueButton.SetActive (false);
         blueButton.GetComponent<Button> ().onClick.AddListener (blueTeam);
         redButton = canvas.transform.GetChild (3).gameObject;
         redButton.SetActive (false);
         redButton.GetComponent<Button> ().onClick.AddListener (redTeam);
 
         text = canvas.transform.GetChild (5).GetComponent<UnityEngine.UI.Text>();
         text.text = textNum.ToString();
 
         prefab = Resources.Load ("cube") as GameObject;
 
         cam = GetComponent<Camera>() ;
 
         if (!isLocalPlayer) {
             cam.enabled = false;
         }
 
         //not mine (Don't edit)
         targetDirection = transform.localRotation.eulerAngles;
     }
 
     public void play(){
         playButton.SetActive (false);
         redButton.SetActive (true);
         blueButton.SetActive (true);
     }
 
     public void blueTeam(){
         Cursor.visible = false;
         playing = true;
         blueButton.SetActive (false);
         redButton.SetActive (false);
         blueTrue = true;
         RenderSettings.skybox = blueBox;
     }
 
     public void redTeam(){
         Cursor.visible = false;
         playing = true;
         blueButton.SetActive (false);
         redButton.SetActive (false);
         blueTrue = false;
         player.GetComponent<MeshRenderer> ().material = red;
         RenderSettings.skybox = redBox;
     }
         
     void Break(){
         if (Time.unscaledTime - timer > 0.5f) {
             RaycastHit hit;
             Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
 
             if (Physics.Raycast (ray, out hit, 100.0f)) {
                 if (hit.transform) {
                     if (sameBlock.name == hit.transform.name) {
                         if (hit.transform.tag == "cube") {
                             CmdNetDestroy (GameObject.Find (hit.transform.name));
                             timer = Time.unscaledTime - 0.25f;
                             dontGo = true;
                             textNum++;
                             sameBlock = null;
                         }
                     } else {
                         timer = Time.unscaledTime - 0.25f;
                         Same ();
                     }
                 }
             }
         }
         return;
     }
 
 
     void CmdNetDestroy(GameObject obj){
         print ("DESTROYED");
         Destroy (obj);
     }
         
     void Place(){
         RaycastHit hit;
         Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
         timer = 0.0f;
 
         if (Physics.Raycast (ray, out hit, 100.0f)) {
             if (hit.transform) {
                 GameObject cube = Instantiate (prefab) as GameObject;
                 cube.name = "cube " + cubes.ToString ();
 
                 cubes++;
 
                 cube.transform.position = hit.point + Vector3.up * 0.5f;
                 if (!blueTrue) {
                     cube.GetComponent<MeshRenderer> ().material = red;
                 }
                 textNum += -1;
 
                 //spawning the cube
                 CmdNetSpawn(cube);
             }
         }
     }
 
     [Command]
     void CmdNetSpawn(GameObject obj){
         print ("PLACED");
         NetworkServer.Spawn (obj);
     }
         
     void Same(){
 
         shootSpeed = 0;
 
         RaycastHit hit;
 
         Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
 
         if (Physics.Raycast (ray, out hit, 100.0f)) {
             if (hit.transform) {
                 sameBlock = hit.transform;
             }
         }
     }
         
     void Shoot(){
         print ("SHOT");
         RaycastHit hit;
 
         Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
 
         if (Physics.Raycast (ray, out hit, 100.0f)) {
             if (hit.transform) {
 
                 Rigidbody rigid = hit.transform.GetComponent<Rigidbody> ();
 
                 if (rigid != null && sameBlock.name == hit.transform.name) {
                     CmdNetMove (player.transform.forward, shootSpeed, rigid);
                     shootSpeed = 10;
                 }
             }
         }
     }
 
     [Command]
     void CmdNetMove(Transform player, float speed, Rigidbody rigid){
         rigid.AddForce (player.forward * speed, ForceMode.Impulse);
     }
 
     void Update(){
         if (!isLocalPlayer){
             return;
         }
         if (playing) {
 
             print ("playing");
             text.text = textNum.ToString ();
 
             //this isn't my code (Don't edit)
             if (lockCursor) {
                 Cursor.lockState = CursorLockMode.Locked;
             }
             var targetOrientation = Quaternion.Euler (targetDirection);
             var mouseDelta = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
             mouseDelta = Vector2.Scale (mouseDelta, new Vector2 (sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
             _smoothMouse.x = Mathf.Lerp (_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
             _smoothMouse.y = Mathf.Lerp (_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
             _mouseAbsolute += _smoothMouse;
             if (clampInDegrees.x < 360)
                 _mouseAbsolute.x = Mathf.Clamp (_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
             if (clampInDegrees.y < 360)
                 _mouseAbsolute.y = Mathf.Clamp (_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
             transform.localRotation = Quaternion.AngleAxis (-_mouseAbsolute.y, targetOrientation * Vector3.right) * targetOrientation;
             var yRotation = Quaternion.AngleAxis (_mouseAbsolute.x, transform.InverseTransformDirection (Vector3.up));
             transform.localRotation *= yRotation;
             //from here on is my code
 
             if (Input.GetMouseButtonDown (1)) {
                 timer = Time.unscaledTime;
                 dontGo = false;
                 Same ();
             }
 
             if (Input.GetMouseButton (1)) {
                 if (sameBlock == null) {
                     Same ();
                 }
 
                 Break ();
             }
 
             if (Input.GetMouseButtonUp (1) && textNum > 0 && !dontGo) {
                 sameBlock = null;
                 Place ();
             }
 
             if (Input.GetMouseButton (0) && shootSpeed < 50) {
                 shootSpeed += 2;
             }
 
             if (Input.GetMouseButtonDown (0)) {
                 Same ();
             }
 
             if (Input.GetMouseButtonUp (0)) {
                 Shoot ();
             }
 
             float h = Input.GetAxis ("Horizontal");
             float v = Input.GetAxis ("Vertical");
             float up = 0;
             if (Input.GetKey (KeyCode.Space)) {
                 up = 1;
             } else {
                 if (Input.GetKey (KeyCode.LeftShift)) {
                     up = -1;
                 }
             }
             
             player.position += v * player.forward * Time.deltaTime * speed;
             player.position += h * player.right * Time.deltaTime * speed;
             player.position += up * Vector3.up * Time.deltaTime * speed;
             if (player.position.y < 0.4f) {
                 Vector3 vect = player.position;
                 vect.y = 0.4f;
                 player.position = vect;
             }
         }
     }
 

Here are the parts where I call commands

         void Break(){
             if (Time.unscaledTime - timer > 0.5f) {
                 RaycastHit hit;
                 Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
     
                 if (Physics.Raycast (ray, out hit, 100.0f)) {
                     if (hit.transform) {
                         if (sameBlock.name == hit.transform.name) {
                             if (hit.transform.tag == "cube") {
                                 CmdNetDestroy (GameObject.Find (hit.transform.name));
                                 timer = Time.unscaledTime - 0.25f;
                                 dontGo = true;
                                 textNum++;
                                 sameBlock = null;
                             }
                         } else {
                             timer = Time.unscaledTime - 0.25f;
                             Same ();
                         }
                     }
                 }
             }
             return;
         }
     
     
         void CmdNetDestroy(GameObject obj){
             print ("DESTROYED");
             Destroy (obj);
         }
             
         void Place(){
             RaycastHit hit;
             Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
             timer = 0.0f;
     
             if (Physics.Raycast (ray, out hit, 100.0f)) {
                 if (hit.transform) {
                     GameObject cube = Instantiate (prefab) as GameObject;
                     cube.name = "cube " + cubes.ToString ();
     
                     cubes++;
     
                     cube.transform.position = hit.point + Vector3.up * 0.5f;
                     if (!blueTrue) {
                         cube.GetComponent<MeshRenderer> ().material = red;
                     }
                     textNum += -1;
     
                     //spawning the cube
                     CmdNetSpawn(cube);
                 }
             }
         }
     
         [Command]
         void CmdNetSpawn(GameObject obj){
             print ("PLACED");
             NetworkServer.Spawn (obj);
         }
             
         void Shoot(){
             print ("SHOT");
             RaycastHit hit;
     
             Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
     
             if (Physics.Raycast (ray, out hit, 100.0f)) {
                 if (hit.transform) {
     
                     Rigidbody rigid = hit.transform.GetComponent<Rigidbody> ();
     
                     if (rigid != null && sameBlock.name == hit.transform.name) {
                         CmdNetMove (player.transform.forward, shootSpeed, rigid);
                         shootSpeed = 10;
                     }
                 }
             }
         }
     
         [Command]
         void CmdNetMove(Transform player, float speed, Rigidbody rigid){
             rigid.AddForce (player.forward * speed, ForceMode.Impulse);
         }


All the parts were I say the code is not mine are because I used a first-person script that I did not write.

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

122 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 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

Object not updating on host 1 Answer

How [Command] and [ClientRpc] are made in that way that when you call them they notify the network to send the parameters but the code inside doesn't get executed. 1 Answer

Command not executing for all clients 0 Answers

Unity UDP freezes if disconnected from server 2 Answers

Learning Project: How is Badumna for a server? 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