Networking - Host can spawn objects but client cant
Hi,
I am trying to learn the new networking in unity. I made a small project where you walk around with a cube and shoot spheres forward on mouse click. I followed at least three tutorials on shooting but i'm running into a strange problem. When the host tries to shoot the [Command] void CmdDoFire() executes and the bullet is visible both for the host and the client. Hovewer, when the client tries to shoot, the [Command] void CmdDoFire() does not execute! There are no errors, the whole function just isn't executed at all. I've read dosens of posts about shooting in unity but no one seemed to have a problem like i do. This is the player class:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerScript : NetworkBehaviour {
float moveSpeed = 6.0f;
float bulletSpeed = 8.0f;
float lookSpeed = 150.0f;
float fireRate = 0.5f;
float nextFire = 0;
Camera camera = null;
Transform head = null;
public GameObject missilePrefab;
void Start ()
{
if (!isLocalPlayer)
return;
head = this.transform.Find ("Head");
camera = Camera.main;
camera.transform.SetParent (head);
camera.transform.localPosition = new Vector3 (0, 0, 0);
camera.transform.rotation = Quaternion.identity;
//Screen.lockCursor = true;
}
void FixedUpdate ()
{
if (!isLocalPlayer)
return;
Vector3 translation = new Vector3 (CrossPlatformInputManager.GetAxis ("Horizontal") * Time.deltaTime * moveSpeed, 0, CrossPlatformInputManager.GetAxis ("Vertical") * Time.deltaTime * moveSpeed);
float rotHorizontal = CrossPlatformInputManager.GetAxis("Mouse X") * Time.deltaTime * lookSpeed;
float rotVertical = -CrossPlatformInputManager.GetAxis("Mouse Y") * Time.deltaTime * lookSpeed;
transform.Translate(translation);
transform.Rotate (0, rotHorizontal, 0);
head.Rotate (rotVertical, 0, 0);
if(CrossPlatformInputManager.GetButton("Fire1") && Time.fixedTime > nextFire)
{
nextFire = Time.fixedTime + fireRate;
CmdDoFire(2.0f);
}
}
[Command]
void CmdDoFire(float lifetime)
{
GameObject missile = (GameObject) Instantiate(missilePrefab, head.position, Quaternion.identity);
Rigidbody rigid = missile.GetComponent<Rigidbody>();
rigid.velocity = head.forward * bulletSpeed;
Destroy(missile, lifetime);
NetworkServer.Spawn(missile);
}
void OnDestroy()
{
Screen.lockCursor = false;
}
}
I am stuck at such a simple thing but I just can't figure out why it doesn't work.
Now I know some people will first mention the simple, often made mistakes. This is what I am sure of:
The missile/bullet prefab is in the networkmanager spawnable list
Both the player and the missile/bullet have network identities and network transforms
I'd really appreciate if someone know something about this kond of problem.
did this script attached to Game Player Prefab of network manager game object.And also make sure local player authority is ticked.
It's a part of the prefab and local player authority is ticked. Thanks for the reply
Your answer
Follow this Question
Related Questions
How can i Spawn a different prefab for client? 0 Answers
MLAPI Client Character Spawning Problem 0 Answers
Problem with instantiating and declaring objects in Netcode on client 0 Answers
How to correctly change scene with a Lobby Manager? 0 Answers
Networking - How do i spawn an object with client authority? 1 Answer