Unet: Bullets only spawn only spawn on host, visible only on host
Hello guys I have been working on a networked location based game using unet. This is my first Unity and Unet project. I have a problem with shooting. For some reason the bullets only spawn if there is only 1 player connected or only on the host if there are some players connected. Also, only the host can shoot, the other players cannot. Looked at it for a while and did some research but I'm sure I miss something. THANKS.
Shooting code: using UnityEngine; using System.Collections; using UnityEngine.Networking; using System.Collections.Generic;
public class PEShooting : NetworkBehaviour {
public GameObject bulletPrefab;
public Transform bulletSpawn;
public float bulletVelocity = 10f;
public PEScoreTracker score;
public int maxBullets = 4;
[SyncVar] private int counterBullet;
void Start()
{
counterBullet = 0;
}
// Update is called once per frame
public void shootBullet () {
if (!isLocalPlayer)
{
return;
}
CmdFire();
}
[Command]
void CmdFire()
{
if (counterBullet < maxBullets)
{
GameObject bullet = (GameObject)Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
bullet.transform.position = bulletSpawn.position;
bullet.transform.rotation = bulletSpawn.rotation;
bullet.gameObject.GetComponent<Bullet>().setOwner(score);
//Debug.Log("BP:"+bulletSpawn.transform.position);
//Debug.Log("B:" + bullet.transform.position);
//bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * bulletVelocity;
counterBullet++;
NetworkServer.Spawn(bullet);
}
}
public void destroyBullet()
{
counterBullet--;
}
}
Bullet Code: using UnityEngine; using System.Collections; using UnityEngine.Networking;
public class Bullet : NetworkBehaviour
{
[SerializeField] public float age;
public float shellLifeTime = 30.0f;
public float bulletSpeed = 50.0f;
public bool isLive = true;
public PEScoreTracker owner;
public int damage = 1;
private void Start()
{
age = 0.0f;
}
public void setOwner(PEScoreTracker shooter)
{
this.owner = shooter;
MeshRenderer thisMesh = gameObject.GetComponent<MeshRenderer>();
thisMesh.material.color = owner.getColor();
}
[ServerCallback]
private void Update()
{
age += Time.deltaTime;
transform.Translate(new Vector3(0,0,bulletSpeed/100));
//if (!isServer)
//{
// return;
//}
if (age > shellLifeTime)
{
destroyBullet();
}
}
[ServerCallback]
private void OnCollisionEnter(Collision collision)
{
if (!isLive)
return;
if (collision.gameObject.tag == "Character" && isServer && collision.gameObject!=owner.gameObject)
{
PEScoreTracker enemy = collision.gameObject.GetComponent<PEScoreTracker>();
if (enemy != null && owner != null)
{
isLive = false;
owner.updateScore(damage);
enemy.updateScore(-damage);
destroyBullet();
}
}
}
private void destroyBullet()
{
owner.destroyBullet();
NetworkServer.Destroy(gameObject);
}
}
Score Tracker: using UnityEngine; using System.Collections; using UnityEngine.Networking; using GoShared; public class PEScoreTracker : NetworkBehaviour { [SyncVar] public int score; public PELocationManager LocManager; public PEShooting shootManager; public int maxBullets = 4; private int counterBullet; private Color playerColor = Color.white; // Use this for initialization void Awake() { score = 0; }
private void Start()
{
playerColor = LocManager.playerColor;
}
public Color getColor()
{
Color copy = this.playerColor;
return copy;
}
public void updateScore(int Damage)
{
score += Damage;
LocManager.scoreUpdater();
}
public void destroyBullet()
{
shootManager.destroyBullet();
}
}
Your answer
Follow this Question
Related Questions
'Spawn scene object not found 1' Error 4 Answers
How to send Kinect skeleton data through Unity networks (multiplayer) 0 Answers
Best Practice for an object that is both online and offline using uNET 0 Answers
Photon Networking - How to move all players from game scene to current room scene? 0 Answers
How to make bullet shoot in same direction as player faces in 2D Top Down Shooter 0 Answers