- Home /
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.
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.
Your answer
Follow this Question
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