Hi guys, i have some problems with bullet instantiation and network spawn.
I have a Player with a “GunController” script and i have a gun with a “Cannon” script (the gun became child of the Player when it walks over it);
When i shoot, the GunController sends a message to the Cannon scrpit to start the CmdShoot method
this is the Cannon script:
using UnityEngine;
using UnityEngine.Networking;
public class Cannon : NetworkBehaviour
{
public string gunName = "Cannon";
public GameObject bulletPrefab;
public float bulletSpeed = 10f;
public float damage = 10f;
public GameObject playerCollided;
public Transform bulletSpawn;
//this checks if the collider is a player and if it have a gun equipped
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Player" && collider.gameObject.GetComponent<GunController>().hasGunEquipped.Equals(false))
{
GunController gunController = collider.gameObject.GetComponent<GunController>();
if (gunController.hasGunEquipped.Equals(false))
{
playerCollided = collider.gameObject;
bulletSpawn = this.transform.Find("BulletSpawn");
BecameChild();
}
}
//the gun becames player's children
void BecameChild()
{
//find the bulletspawn point
Transform bulletSpawnTag = playerCollided.transform.Find("bulletSpawnTag");
//set this weapon as child of the player
transform.SetParent(playerCollided.GetComponent<Transform>());
transform.SetPositionAndRotation(trace.GetComponent<Transform>().position, trace.transform.rotation);
//set this weapon in the player GunController
playerCollided.GetComponent<GunController>().hasGunEquipped = true;
playerCollided.GetComponent<GunController>().gunEquippedName = this.gunName;
}
[Command]
private void CmdShoot()
{
//instatiate bulletPrefab
GameObject bulletTemp = Instantiate(bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation) as GameObject;
//set bullet damage and speed
bulletTemp.GetComponent<bullet>().SetDamageAndBulletSpeed(damage, bulletSpeed);
//add force the bullet's rigidbody
bulletTemp.GetComponent<Rigidbody>().AddForce(bulletTemp.forward * bulletSpeed, ForceMode.Impulse);
NetworkServer.Spawn(bulletTemp);
Destroy(bulletTemp, 2f);
}
}
With this setup when a LAN client shoot, no force is applied to the bullet locally, but it does on the LAN Host. instead when the LAN Host shoots, locally the bullet gets the force applyed,but the LAN clients doesn’t see the bullet moving.
I made it works appliyng the force directly into the Start method of the bullet script, but it this case i can’t set the bulletSpeed variable and I should hardcode it (So i must create a bullet prefab for each weapon to change it’s damage).
If i create a method into the bullet to apply the foce, and i call it into the CmdShoot() the bullet does’move locally neither on LAN Client or Lan Host.
Please help me figure out what i’m doing wrong :l
(sorry for bad english xD)