- Home /
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);
}
}
Your answer
Follow this Question
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