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 nickostan · Aug 17, 2017 at 02:00 PM · networkingclientclient-servercommandhost

[Command] not sending to server?

Hi,

I'm trying to get the basics of networking down for a simple game I've made. I followed the online unity tutorial that teaches you how to do so.

Currently, my client and host control properly in their movement (host controls host, client controls client).

As soon as I tried to apply the info in the tutorial to other aspects of my game (spawning things, jumping, etc), things got super wacky.

My problems boils down to... when I add the [Command] part into my code, it simply doesnt send to the server

I've made a gif for you to visualize these problems:

http://imgur.com/a/Fo8aW

I'm showing here that

a) though the client and host move independent of each other, the direction of both characters change on the host, and neither change on the client b) when a teleport ball is shot, it spawns from both players, though this is only seen on the host, and the client character is unaffected by it c) The host can only jump once, due to groundcheck rules. The client ignores the groundcheck.

Here are screenshots of my network components:

alt text

And as for the teleball script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class Nawlins : NetworkBehaviour {
 
     PlayerController _pc;
 
     Transform playerTransform;
     Camera viewCamera;
 
     //Skills?
     public GameObject projectileOne;
     public float projectileOneForce;
     public GameObject projectileTwo;
     public float projectileTwoForce;
 
     public float swipeCooldown;
     public float swipeVelocity;
     float swipeAvailableAt;
 
     public float teleBallCooldown;
     float teleBallAvailableAt;
 
 
     // Use this for initialization
     void Start () {
         _pc = GetComponent<PlayerController>();
         viewCamera = _pc.viewCamera;
         playerTransform = _pc.playerTransform;
         swipeAvailableAt = 0.0F;
         teleBallAvailableAt = 0.0F;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (!isLocalPlayer)
         {
             return;
         }
 
         CmdClickListen();
     }
 
     [Command]
     void CmdClickListen()
     {
         if (Input.GetMouseButtonDown(0) && swipeAvailableAt < Time.time) //Nawlins' left click is a dash/swipe
         {
             swipeAvailableAt = Time.time + swipeCooldown;
             int direction;
             if (_pc.isFacingRight)
             {
                 direction = 1;
             }else
             {
                 direction = -1;
             }
 
             _pc.rb.AddForce(Vector2.right * direction * swipeVelocity);
             //_pc.rb.velocity = Vector2.right * direction * swipeVelocity;
         }
         if (Input.GetMouseButtonDown(1) && teleBallAvailableAt < Time.time) //right click
         {
             teleBallAvailableAt = Time.time + teleBallCooldown;
 
             Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
 
             Vector3 clickDirection = (ray.origin - transform.position);
             clickDirection.z = 0;
             Vector3 normal = clickDirection.normalized;
 
             GameObject clone = (GameObject)Instantiate(projectileTwo, transform.position, transform.rotation);
             clone.layer = 11;
             clone.GetComponent<Projectile>().owner = playerTransform;
             clone.GetComponent<Rigidbody2D>().AddForce(normal * projectileTwoForce);
 
             NetworkServer.Spawn(clone);
         }
     }
 }
 

help2.png (121.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unidad2pete · Aug 17, 2017 at 03:41 PM

Command functions is not for get inputs, is only for code to server, you have to make all code on your Pc, and use Command to Server functions and send data to other players.

Try this;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class Nawlins : NetworkBehaviour
 {
 
     PlayerController _pc;
 
     Transform playerTransform;
     Camera viewCamera;
 
     //Skills?
     public GameObject projectileOne;
     public float projectileOneForce;
     public GameObject projectileTwo;
     public float projectileTwoForce;
 
     public float swipeCooldown;
     public float swipeVelocity;
     float swipeAvailableAt;
 
     public float teleBallCooldown;
     float teleBallAvailableAt;
 
 
     // Use this for initialization
     void Start()
     {
         _pc = GetComponent<PlayerController>();
         viewCamera = _pc.viewCamera;
         playerTransform = _pc.playerTransform;
         swipeAvailableAt = 0.0F;
         teleBallAvailableAt = 0.0F;
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (!isLocalPlayer)
         {
             return;
         }
 
         if (Input.GetMouseButtonDown(0) && swipeAvailableAt < Time.time) //Nawlins' left click is a dash/swipe
         {
             swipeAvailableAt = Time.time + swipeCooldown;
             int direction;
             if (_pc.isFacingRight)
             {
                 direction = 1;
             }
             else
             {
                 direction = -1;
             }
 
             _pc.rb.AddForce(Vector2.right * direction * swipeVelocity);
             //_pc.rb.velocity = Vector2.right * direction * swipeVelocity;
         }
         if (Input.GetMouseButtonDown(1) && teleBallAvailableAt < Time.time) //right click
         {
             teleBallAvailableAt = Time.time + teleBallCooldown;
 
             Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
 
             Vector3 clickDirection = (ray.origin - transform.position);
             clickDirection.z = 0;
             Vector3 normal = clickDirection.normalized;
             CmdClickListen(normal);
             
         }
     }
 
     [Command]
     void CmdClickListen(Vector3 normal)
     {
         GameObject clone = (GameObject)Instantiate(projectileTwo, transform.position, transform.rotation);
         NetworkServer.Spawn(clone);
 
         RpcClickListen(normal, clone);
 
         
 
         
     }
     [ClientRpc]
     public void RpcClickListen(Vector3 normal, GameObject clone)
     {
         clone.layer = 11;
         clone.GetComponent<Projectile>().owner = playerTransform;
         clone.GetComponent<Rigidbody2D>().AddForce(normal * projectileTwoForce);
     }
 }

You call Cmd Function and send your Vector3 normal to make other players to know that vector. On command, Server spawn the objects, that code is runing on server, and he spawn on all clients, and call Rpc function to say other players new info. Rpc function say to all clients, the new gameObject get a layer 11, player owner and new Force applied. Im not tested but should be work fine.

Comment
Add comment · Share
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

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

96 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

Related Questions

Networking bullet spawning issues between Host/Client 0 Answers

Command not executing for all clients 0 Answers

Why is the command function not being called on the client? 0 Answers

Why will the raycast not work on the client? 2 Answers

How to send commands from client objects without authority? 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