Client's object spawn position and rotation network bug?
After hours of testing and experimenting, I am sure that my bullet won't spawn at the position and rotation of my bullet spawn game BUT not as a host. What happens is if Player click to shoot then Bullet spawn from game object "BulletSpawn" with position and rotation of the BulletSpawn and the game is a first person with mouse look so that when I look left right up or down the BulletSpawn will look there so bullets will go straight at the center of the screen. But the problem is that if i am a client the bullets somehow managed to spawn but not in the position and rotation of the BulletSpawn even the BulletSpawn rotated. This doesn't happen if I spawn as the host for some reason. Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ShootBullet : NetworkBehaviour {
public GameObject Bullet;
public Transform BulletSpawn;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer) {
GetComponent<ShootBullet> ().enabled = false;
}
if (Input.GetButtonDown ("Fire1")) {
CmdFire ();
Debug.Log("Shooting");
}
}
[Command]
void CmdFire () {
Debug.Log("Shot");
GameObject bullet = (GameObject)Instantiate (Bullet, BulletSpawn.position, BulletSpawn.rotation);
bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 10.0f;
NetworkServer.Spawn(bullet);
}
}
Your answer
Follow this Question
Related Questions
Bullets not spawning in other clients but its spawning on the client that shot it. 1 Answer
Problem with instantiating and declaring objects in Netcode on client 0 Answers
How can I make a simple Lobby scene, while staying in the same scene all the time? 0 Answers
Raycast doesnt work for client in netcode 0 Answers
How should I verify that all of my client Object are created and ready after loading a new scene? 0 Answers