• 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 danilorios · Oct 05, 2016 at 05:26 AM · cameramultiplayerspawnclickclient

Multiplayer click and spawn

Hello everyone. I'm trying to make a multiplayer tower defense game which each player can spawn towers as long as their have money. Everything's working fine except for one thing: the second player can't use his/her camera. I'm using the default NetworkManager to spawn players which are the cameras. Each camera has a NetworkIdentity with Local Player Authority check and NetworkTransform. Same thing with the cube i'm trying to spawn. The camera uses raycast to click on stuff and get position.

So I'm using a [Command] function to manipulate the raycast and the spawn. Looks like only the host can move and spawn objects because the client can't enter this Cmd function. Then, I tried to use [ClientRpc], however it seems that clients can't call this method. I now find myself lost and don't know what to do. How the client is going to send information to the sever? How's the game could know where's he or she is pointing at and be able to spawn an object for both players?

Any suggestions?

Thanks in advance. Here's the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class SpawnObjectOnClick : NetworkBehaviour {
     Ray myRay;
     RaycastHit hit;
     public GameObject cube;
 
     void Update(){
         if (!isLocalPlayer) {
             return;
         }
         CmdGetPosition ();
     }
 
     [Command]
     void CmdGetPosition(){
         myRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (myRay, out hit)) {
             if (Input.GetMouseButtonDown (0)) {
                 GameObject newObject = (GameObject)Instantiate (cube, hit.point, Quaternion.Euler (0, 90, 0));
                 NetworkServer.Spawn(newObject);
             }
         }
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by danilorios · Oct 11, 2016 at 06:52 PM

Hi guys, found a solution!

 using UnityEngine;
 using System;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class SetTowerOnline : NetworkBehaviour
 {
     public int selected;
     public GameObject towers;
     public float[] prices;
     public GameObject tile;
     public Vector3 PositionGetter;
     public Vector3 pos;
     public GameObject bulletPrefab;
     public Transform bulletSpawn;
     private Money moneyScript;
 
     Camera mainCamera;
 
     void Start ()
     {
         mainCamera = GetComponent<Camera>();
         this.moneyScript = GameObject.Find ("GameLogic").GetComponent<Money> ();
     }
 
     void Spawn(){
             Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
             Debug.Log ("Raycast disparado!");
 
             if (Physics.Raycast (ray, out hit, 20)) {
                 if (hit.transform.tag == "tile") {
                     this.tile = hit.transform.gameObject;
                 Debug.Log ("Estou no tile");
                 } else {
                     this.tile = null;
                 Debug.Log ("Estou fora do tile");
                 }
             }
 
             if (Input.GetMouseButtonDown (0) && this.tile != null) {
             TileTaken tileTakenOnlineScript = this.tile.GetComponent<TileTaken> ();
             if (!tileTakenOnlineScript.isTaken && (this.moneyScript.money - prices [this.selected]) >= 0) {
                 this.moneyScript.money -= prices [this.selected];
                 pos = new Vector3 (this.tile.transform.position.x, 1.1f, this.tile.transform.position.z);
                 PositionGetter = pos;
                 GameObject newObject = (GameObject)Instantiate (towers, transform.position, Quaternion.Euler (0, 90, 0));
                 newObject.transform.position = pos;
 
                 CmdClientSpawn ();
                 if (selected != 3) {
                     tileTakenOnlineScript.isTaken = true;
                 }
             }
         } 
     }
 
     [Command]
     void CmdClientSpawn(){
         GameObject newObject = (GameObject)Instantiate (towers, transform.position, Quaternion.Euler (0, 90, 0));
         newObject.transform.position = PositionGetter;
         NetworkServer.Spawn (newObject);
 
     }
 
     void Update ()
     {
         if (!isLocalPlayer)
         {
             return;
         }
         Spawn ();        
     }
 }

Commands send information from client to server. So, apparently, you should not deal with the client raycast on the host. The only thing you should do is to send a instantiate position and what you want to instantiate to the server. I was thinking that the client wasn't getting to the Cmd function, but it was, only I coundn't see, not from client pespective anyway. The only problem now is that the host is not receiving the PositionGetter value and is spawning objects on default position. For this, guess I going to create a function for each possible position, since is not that many.

If anyone's interested, this is the tutorial for tower defence I following: https://www.youtube.com/watch?v=voBaE3ED510 And for multiplayer: https://www.youtube.com/watch?v=ovUnNJlREIk Both are very good tutorials.

Comment

People who like this

0 Show 0 · 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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Starting Arsenal in multiplayer 1 Answer

Multiplayer - Players have wrong cameras 1 Answer

Unet spawn a camera? 0 Answers

problem in unity multiplayer about "ccu" It is limited to 200 ccu 1 Answer

Why can't the client move a networked object ? 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