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 remo_10 · May 15, 2020 at 06:44 AM · networkingmultiplayer-networkingserver-hosting

UNET: All players spawned can be controlled only by one person

This is my first post here, I am creating a multiplayer game with UNet (I know its obsolete at this point) but I am at a point where I know a bit about setting up the multiplayer game like making a network manager, UI etc but my players are getting controlled by one person, which is the server host. Here is the movement script for the player.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 
 public class Player : NetworkBehaviour
 {
     Vector3 inputValue;
     float speed = 10f;
     float rotateSpeed = 1f;
     //public Vector3 point;
 
 Vector3 inputValue;
     float speed = 10f;
     float rotateSpeed = 1f;
     //public Vector3 point;
 
     //public GameObject bulletPrefab; // spawnable object
     //public Transform bulletSpawn;
     //public Transform[] bulletLocations; // lsit of spawnable locations
     //public float spawnTime = 3f;
 
 
 
     int bulletNum = 0;
     private bool rightHandSide;
 
     //Cannon variables
     public GameObject projectile;
     public Transform[] rightGuns;
     public Transform[] leftGuns;
     public Transform[] otherGuns;
 
     // UI Variables
     public Image healthBar;
     public Text healthPer;
     public Image armourBar;
     public Text armourPer;
     public Text timerText;
     public Text scoreText;
 
     // Health Variables
     private float health = 1f;
     public float Health { get { return health; } set { health = value; } }
     private float armour = 1f;
     public float Armour { get { return armour; } set { armour = value; } }
 
     // Time variables
     private float time = 0f;
     private string timer;
 
     // Score variables
     private int score;
     public int Score { get { return score; } set { score = value; } }
 
 
     // Start is called before the first frame update
     void Start()
     {
         //InvokeRepeating("Spawn", spawnTime, spawnTime);
     }
 
     // Update is called once per frame
     void Update()
     {
         #region Commented out/Old code
         /*if(Input.mousePosition.x > Screen.width / 2)
         {
             rightHandSide = true;
         }
         else
         {
             rightHandSide = false;
         }
        
         inputValue.x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         //inputValue.y = -Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         inputValue.z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         transform.Translate(inputValue);
 
         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);*/
 
         /*if (Input.GetKey(KeyCode.A))
         {
             transform.RotateAround(point, Vector3.up, 5 * Time.deltaTime);
         }
         else if (Input.GetKey(KeyCode.D))
             transform.RotateAround(point, -Vector3.up, 5 * Time.deltaTime);*/
 
         //spawnIndex = Random.Range(0, bulletLocations.Length);
         #endregion
 
         if (!isLocalPlayer)
         {
             return;
         }
 
         if (Input.mousePosition.x > Screen.width / 2)
         {
             rightHandSide = true;
         }
         else
         {
             rightHandSide = false;
         }
 
         inputValue.x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         //inputValue.y = -Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         inputValue.z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         transform.Translate(inputValue);
 
         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
 
         healthBar.fillAmount = health;
         armourBar.fillAmount = armour;
         timerText.text = timer;
         scoreText.text = score.ToString();
 
         CmdFire();
 
         healthPer.text = Mathf.Round(health * 100) + "%";
         healthBar.color = Lerp3(Color.red, Color.yellow, Color.green, health);
 
         armourPer.text = Mathf.Round(armour * 100) + "%";
 
         time += Time.deltaTime;
         timer = string.Format("{0:00}:{1:00}", time / 60, Mathf.Floor(time % 60));
        
         //print(Health);
     }
 
     public override void OnStartLocalPlayer()
     {
         //GetComponentInChildren<Camera>().enabled = true;
     }
 
     [Command]
     void CmdFire()
     {
 
         if (Input.GetMouseButtonDown(0))
         {
             if (rightHandSide)
             {
                 FireCannons(rightGuns, otherGuns);
             }
             else
             {
                 FireCannons(leftGuns, otherGuns);
             }
         }
     }
 
     private Color Lerp3(Color a, Color b, Color c, float t)
     {
         if (t < 0.5f)
             return Color.Lerp(a, b, t / 0.5f);
         else
             return Color.Lerp(b, c, (t - 0.5f) / 0.5f);
     }
 
     void FireCannons(Transform[] guns, Transform[] other)
     {
         if (!isLocalPlayer)
         {
             return;
         }
 
         foreach (Transform spawner in guns)
         {
             Instantiate(projectile, spawner.position, spawner.rotation);
         }
 
         foreach (Transform spawner in other)
         {
             Instantiate(projectile, spawner.position, spawner.rotation);
         }
     }
 
     public void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Debris")
         {
             health = health - 0.1f;
             print(health);
             Destroy(other.gameObject);
         }
     }
 }

When testing, the player host (left build screen) can move around by itself, can shoot (all well and good). When the second player joins in, the turrets of the first player move at the same time when the second player does. When the first player shoots, the second player doesn't show it has but the first player can see the second player shoots the bullets but the second player can't shoot, however.

alt text

Whenever the first player moves (left side), the second player moves according to the first player and the second player's screen is lagging.

I would like to know how can I spawn players that can be controlled separately in the server instead of one controlling the other. Also, the UI is messed up which doesn't matter in this case.

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

179 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Is there a similar solution to UNET pay per traffic with Multiplay? 1 Answer

Best way to choose Google Play Realtime Multiplayer Host? 0 Answers

Network command is running on client? 1 Answer

Photon Networking not synchronizing 0 Answers

RPC and Command Calls OR Network messages for syncing variables across Server and Client? 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