Answer the question
In order to leave comments, you need to log in
[Unity] Shooting in multiplayer?
Hello!
I have a PlayerShoot script:
This is one of its methods:
[Command]
void CmdShoot()
{
// Create the Bullet from the Bullet Prefab
GameObject bullet = Instantiate(
bulletPref,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 700;
//// Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
void Shoot()
{
// Create the Bullet from the Bullet Prefab
GameObject bullet = Instantiate(
bulletPref,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 700;
//// Spawn the bullet on the Clients
// NetworkServer.Spawn(bullet);
CmdSpawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
[Command]
void CmdSpawn(GameObject bullet)
{
//Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
}
Answer the question
In order to leave comments, you need to log in
Colleague, you are a star. Truth. As a result of the torment, you will come to the idea that the time on the server is the present, and the time on the client is always the past. And it is late by 2 * (ping to the server). Here is a brief description of how Valve implemented it.
There is a unity asset pack with weapons already configured for shooting.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question