Search Unity

Unity UNet: I control the other player but shooting works with spawned player

Discussion in 'UNet' started by remo_10, May 14, 2020.

  1. remo_10

    remo_10

    Joined:
    Aug 17, 2019
    Posts:
    1
    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. The problem is with "isLocalPlayer". I have used "isLocalPlayer" and I had partial success.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6. using UnityEngine.UI;
    7.  
    8. public class Player : NetworkBehaviour
    9. {
    10.     Vector3 inputValue;
    11.     float speed = 10f;
    12.     float rotateSpeed = 1f;
    13.     //public Vector3 point;
    14.  
    15.     //public GameObject bulletPrefab; // spawnable object
    16.     //public Transform bulletSpawn;
    17.     //public Transform[] bulletLocations; // lsit of spawnable locations
    18.     //public float spawnTime = 3f;
    19.  
    20.     int bulletNum = 0;
    21.     private bool rightHandSide;
    22.  
    23.     //Cannon variables
    24.     public GameObject projectile;
    25.     public Transform[] rightGuns;
    26.     public Transform[] leftGuns;
    27.     public Transform[] otherGuns;
    28.  
    29.     // UI Variables
    30.     public Image healthBar;
    31.     public Text healthPer;
    32.     public Image armourBar;
    33.     public Text armourPer;
    34.     public Text timerText;
    35.     public Text scoreText;
    36.  
    37.     // Health Variables
    38.     private float health = 1f;
    39.     public float Health { get { return health; } set { health = value; } }
    40.     private float armour = 1f;
    41.     public float Armour { get { return armour; } set { armour = value; } }
    42.  
    43.     // Time variables
    44.     private float time = 0f;
    45.     private string timer;
    46.  
    47.     // Score variables
    48.     private int score;
    49.     public int Score { get { return score; } set { score = value; } }
    50.  
    51.  
    52.     // Start is called before the first frame update
    53.     void Start()
    54.     {
    55.         //InvokeRepeating("Spawn", spawnTime, spawnTime);
    56.     }
    57.  
    58.     // Update is called once per frame
    59.     void Update()
    60.     {
    61.         #region Commented out/Old code
    62.         /*if(Input.mousePosition.x > Screen.width / 2)
    63.         {
    64.             rightHandSide = true;
    65.         }
    66.         else
    67.         {
    68.             rightHandSide = false;
    69.         }
    70.        
    71.         inputValue.x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    72.         //inputValue.y = -Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    73.         inputValue.z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
    74.         transform.Translate(inputValue);
    75.  
    76.         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);*/
    77.  
    78.         /*if (Input.GetKey(KeyCode.A))
    79.         {
    80.             transform.RotateAround(point, Vector3.up, 5 * Time.deltaTime);
    81.         }
    82.         else if (Input.GetKey(KeyCode.D))
    83.             transform.RotateAround(point, -Vector3.up, 5 * Time.deltaTime);*/
    84.  
    85.         //spawnIndex = Random.Range(0, bulletLocations.Length);
    86.         #endregion
    87.  
    88.         if (!isLocalPlayer)
    89.         {
    90.             return;
    91.         }
    92.  
    93.         if (Input.mousePosition.x > Screen.width / 2)
    94.         {
    95.             rightHandSide = true;
    96.         }
    97.         else
    98.         {
    99.             rightHandSide = false;
    100.         }
    101.  
    102.         inputValue.x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    103.         //inputValue.y = -Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    104.         inputValue.z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
    105.         transform.Translate(inputValue);
    106.  
    107.         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
    108.  
    109.         healthBar.fillAmount = health;
    110.         armourBar.fillAmount = armour;
    111.         timerText.text = timer;
    112.         scoreText.text = score.ToString();
    113.  
    114.         CmdFire();
    115.  
    116.         healthPer.text = Mathf.Round(health * 100) + "%";
    117.         healthBar.color = Lerp3(Color.red, Color.yellow, Color.green, health);
    118.  
    119.         armourPer.text = Mathf.Round(armour * 100) + "%";
    120.  
    121.         time += Time.deltaTime;
    122.         timer = string.Format("{0:00}:{1:00}", time / 60, Mathf.Floor(time % 60));
    123.        
    124.         //print(Health);
    125.     }
    126.  
    127.     public override void OnStartLocalPlayer()
    128.     {
    129.         //GetComponentInChildren<Camera>().enabled = true;
    130.     }
    131.  
    132.     [Command]
    133.     void CmdFire()
    134.     {
    135.  
    136.         if (Input.GetMouseButtonDown(0))
    137.         {
    138.             if (rightHandSide)
    139.             {
    140.                 FireCannons(rightGuns, otherGuns);
    141.             }
    142.             else
    143.             {
    144.                 FireCannons(leftGuns, otherGuns);
    145.             }
    146.         }
    147.     }
    148.  
    149.     private Color Lerp3(Color a, Color b, Color c, float t)
    150.     {
    151.         if (t < 0.5f)
    152.             return Color.Lerp(a, b, t / 0.5f);
    153.         else
    154.             return Color.Lerp(b, c, (t - 0.5f) / 0.5f);
    155.     }
    156.  
    157.     void FireCannons(Transform[] guns, Transform[] other)
    158.     {
    159.         if (!isLocalPlayer)
    160.         {
    161.             return;
    162.         }
    163.  
    164.         foreach (Transform spawner in guns)
    165.         {
    166.             Instantiate(projectile, spawner.position, spawner.rotation);
    167.         }
    168.  
    169.         foreach (Transform spawner in other)
    170.         {
    171.             Instantiate(projectile, spawner.position, spawner.rotation);
    172.         }
    173.     }
    174.  
    175.     public void OnTriggerEnter(Collider other)
    176.     {
    177.         if (other.gameObject.tag == "Debris")
    178.         {
    179.             health = health - 0.1f;
    180.             print(health);
    181.             Destroy(other.gameObject);
    182.         }
    183.     }
    184. }
    185.  
    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.

    upload_2020-5-14_17-22-20.png

    I would like to know what have I made mistakes on in my movement code and if possible some code to solve the issue. Also, the UI is messed up which doesn't matter in this case.