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 /
  • Help Room /
avatar image
0
Question by kuglera · Apr 24, 2017 at 08:58 AM · unity 5networkingshootingbulletspawning problems

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();
     }
 }
 
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

175 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

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


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