I am making a multiplayer game with prefab
shooting, but unfortunately the prefab sometimes spawns in the wrong position when I shoot from a client. The bullet tends to spawn in a location where the player was a few miliseconds ago, making that the bullet sometimes spawns inside of the player which killing the player in the process.
For shooting, I currently have this code setup to instantiate the bullet
void Update() {
if (Input.GetMouseButtonDown(0)) {
float angle = GetComponentInChildren<Weapon>().angle;
CmdShootBullet(angle);
rigidbody.AddForce(Quaternion.AngleAxis(angle + 180, Vector3.forward) * Vector3.right * (bulletPrefab.GetComponent<Bullet>().force / 7.5f));
}
}
[Command]
void CmdShootBullet (float angle) {
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity);
Bullet bulletScript = bullet.GetComponent<Bullet>();
bulletScript.shotBy = gameObject;
bullet.GetComponent<Rigidbody>().AddForce(Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right * bulletScript.force);
NetworkServer.Spawn(bullet);
}
And this is the setup for the location of the player and the gun. The top one is the player, the bottom is the gun.
My guess is that it has to do with lag, that the player position does not update soon enough, so how would I make it that the bullet spawns in the position of the player in the client rather than the position of the player in the host?