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 /
avatar image
0
Question by Watchers · Nov 03, 2016 at 01:01 PM · networkingspawnclientclient-serverhost

Networking bullet spawning issues between Host/Client

Hello guys,

Amateur dev here creating a multiplayer android game app.

Recently I changed the way my bullets receive values and transfer them over to the script that moves them in my multiplayer game. For some reason after this change something weird happened.

Say I m the host, I touch the screen and the bullet moves towards that position and beyond. When the client tries to shoot by touching the screen his bullet will move towards the last position the host touched. I don't know what triggers this change exactly but I ll post my code and pictures of the project.

Basically the PlayerController script attatched to the player takes value from GetShootingPos which is a script attatched to the main camera of the player. After that the playercontroller spawns the bullets on the client [Check CmdFire() ] and then the BulletMover script which is attatched to the bullet takes the value of the getShootingPos which is saved on the playercontroller as the vector3 variable mover and it moves the bullet.

Finally we have another script called playersetup which disabled the player controller for non local players along with their camera and audio listeners. The networkmanager starts is on an offline scene just for reference.

Any help would be great, I am stuck with this one for a while.


 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 
 
 public class PlayerController : NetworkBehaviour {
 
 
     // FPS DISPLAY VAR
     private float deltaTimer=0.0f;
     public float moveSpeed=1.0f;
     private float savedSpeed;
     //public float drag=0.5f;
     public float terminalRotationSpeed = 25.0f;
     public Vector3 MoveVector{set;get;}
     public Vector3 DragVector{ set; get;}
     Vector3 decreaser;
     public Joystick joystick; // get a first value so it debugger wont cry specifically the prefab value of the 
     // joystick we are going to instantiate
     GameObject myObject;
 
 
 
     //==== FOR SHOOTING ======
     //[HideInInspector]
     private GameObject shootPosObject;
     public GameObject shotprefab;
     public  Transform shotSpawn;
 
     public float fireRate;
     private float nextFire;
     public float bulletspeed;
 
 
     //experimental
     public  Vector3 mover ;
 
     //==== FOR SHOOTING ======
 
     private bool iShot= false;
 
     private Rigidbody rigid;
 
     
     private void Update ()
     {
         GetNetworkSendInterval ();
         deltaTimer += (Time.deltaTime - deltaTimer) * 0.1f; // for fps display
 
         if (PauseMenu.IsOn)
             return;
         
         if (!isLocalPlayer)
             return;
 
         if ((Input.GetButton("Fire2") || Input.touchCount == 2 ) && Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             //iShot = true;
             //Debug.Log ("NOW IT'S " + iShot);
             StartCoroutine (FireBullet ()); // waiting so we get the correct value on BulletMover
             //StartCoroutine (WaitTimeGeneral (nextFire-0.01f));
         }
 
      
 
     }
 
     IEnumerator WaitTimeGeneral(float time)
     {
         yield return new WaitForSeconds (time);
         //iShot = false;
     }
 
 
     IEnumerator GeneralInit (float time)
     {
         yield return new WaitForSeconds (time);
 
         //JOYSTICK INIT
         Debug.Log ("Joystick Prefab Loaded in Scene");
         Debug.Log ("Ready");
         myObject = GameObject.FindWithTag("Joystick");
         joystick = myObject.GetComponent<Joystick> ();
         //JOYSTICK INIT
 
         // CAMERA SHOOTER INIT
         shootPosObject = GameObject.FindWithTag("MainCamera");
         // CAMERA SHOOTER INIT
 
     }
 
 
     IEnumerator FireBullet()
     {
         yield return null;  // Wait one Frame 
         GetShootingPos shootScript = shootPosObject.GetComponent<GetShootingPos> ();
         //Debug.Log (shootScript.ShootPos);
 
         mover = shootScript.ShootPos;
         mover = (mover - transform.position).normalized;
         Debug.Log ("PlayerC Mover" + mover);
         CmdFire();
     }
 
 
 
         
     [Command] // <--server
     void CmdFire()
     {
         
 
         // create the bullet object from the bullet prefab
 
         var bullet = (GameObject)Instantiate(shotprefab, shotSpawn.position, transform.rotation);
         //spawn the bullet on the clients
         NetworkServer.Spawn(bullet);
         // when the bullet is destroyed on the server it will automaticaly be destroyed on clients
         Destroy(bullet, 5.0f);    
 
 
     }
 
 
     public  Vector3 Mover
     {
         get { return mover;}
 
         //set { mover = value; }
     }
 
 


 using UnityEngine;
 using System.Collections;
 
 public class GetShootingPos : MonoBehaviour {
     Camera camera;
 
     private RaycastHit hit;
     private GameObject myObject;
     public  Vector3 posx;
     public  Vector3 shootpos;
 
     void Start() {
         camera = GetComponent<Camera>();
     }
 
     void Update() {
         if (Input.touchCount > 0) {
 
             foreach (Touch touch in Input.touches) {
 
 
                 if (Input.touchCount == 2 && touch.fingerId==1) {
                     Ray ray = camera.ScreenPointToRay (touch.position);
                     //Debug.DrawRay (camera.transform.position, touch.position, Color.green);
                     if (Physics.Raycast (ray, out hit)) {
                         //Debug.Log (hit.transform.position.x);
                         posx = hit.transform.position;
                         posx.y = 0.0f;
                         shootpos = posx;
                     }
 
 
                 }
             }
         }
 
 
     }
 
 
     public  Vector3 ShootPos
     {
         get { return shootpos;}
 
         //set { ShootPos = value; }
     }
 }
 



 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 public class BulletMover : NetworkBehaviour {
     public float speed;
     // Use this for initialization
     public Vector3 myTarget;
     void Start () {            
     
 
 
         GameObject playerController = GameObject.FindGameObjectWithTag ("Player");
         PlayerController pcScript = playerController.GetComponent<PlayerController>();
         myTarget = pcScript.Mover;
         Debug.Log ("Bmover Mover " + myTarget);
 
 
 
 
     }
 
 
     
     // Update is called once per frame
     [ClientCallback]
     void Update () {
         //transform.position = Vector3.MoveTowards(transform.position,myTarget,Time.deltaTime*speed);
 
         transform.position += myTarget * speed * Time.deltaTime;
         //transform.Translate(myTarget,Space.World);
     }
 }
 


alt text alt text

player.png (100.9 kB)
bullet.png (121.6 kB)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why will the raycast not work on the client? 2 Answers

[Command] not sending to server? 1 Answer

How to watch in client applications what it is happening in the server one? 0 Answers

Client with authority can't send information to server 0 Answers

NETWORKING: Start as Host if no Host; Otherwise Start as Client 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