Multiplayer shooting script doesn’t work…

Updated on April 15, 2018 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on April 15, 2018

Hello! I just created a shooting script, but even after reading the documentation and watching literally every single video about it (including Brackeys video), I just can’t make it working, so I hope there is someone who can help me out. When the host shoot, other player can’t even see it, but when the client player shoots, both players can see it, but it doesn’t move. Plus if a bullet is destroyed, I won’t be able to spawn new bullets, because Unity gives me an error, that I didn’t register the prefab, but I did… Here is the code:

 

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 public class Player : NetworkBehaviour {
     public int maxHp;
     [SyncVar]
     public int curHp;
     public float attackSpeed;
     public int movementSpeed;
     public int jumpHeight;
     [SyncVar]
     public float bulletspeed;
     [SyncVar]
     public float builletLifetime;
     public int damage;
     
     Vector2 direction;
     
     Vector2 speed;
     bool canAttack;
     public GameObject projectile;
     public GameObject projectileStartPoint;
     //BOOL FOR CC
     public bool canMove = true;
     public bool ableToAttack = true;
     void Start ()
     {
         if (!isLocalPlayer)
             return;
         ClientScene.RegisterPrefab(gameObject);
         canAttack = true;
     }
     void Update ()
     {
         if (!isLocalPlayer)
             return;
         //Attack
         if (Input.GetMouseButton (0))
             if(ableToAttack)
                 if (canAttack)
                     StartCoroutine(StartAttack());
     }
     [Command]
     void CmdAttack ()
     {
         GameObject current = Instantiate (projectile, projectileStartPoint.transform.position, new Quaternion(0, 0, 0, 0));
         current.GetComponent().velocity = speed;
         NetworkServer.Spawn(current);
     }
     //Attackspeed
     public IEnumerator StartAttack ()
     {   
         canAttack = false;
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         direction = new Vector2(mousePos.x - transform.position.x, mousePos.y - transform.position.y);
         speed = direction * bulletspeed * Time.deltaTime;
         CmdAttack();
         yield return new WaitForSeconds (attackSpeed);
         canAttack = true;
     }
 }
  • Liked by
Reply