Hello!
So I’m creating a simple multiplayer online game.
When there is a player, it has the actual player and an invisible object called “BulletSpawnPoint” which is a little out from the player so when I shoot a bullet, it will not touch my player to damage to him.
So, this is my current code:
if (Input.GetKeyDown(KeyCode.Q)) {
if(Time.time > ability1Cooldown){
ability1Cooldown = Time.time + 5;
//TODO: Shoots two balls with two players at only one bullet spawn point.
Rigidbody bulletfire = Network.Instantiate(fireBallProjectile, GameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity, 0) as Rigidbody;
bulletfire.AddForce(transform.forward * 350);
} else {
audio.PlayOneShot(cancelAudio);
}
}
So when I test it with two players online. It will create two fireball object at one spawn point (of player1)
And when player2 tries to press Q, two fireballs at player1’s bulletspawnpoint gets created.
How can I fix this?
I want it to spawn at that object bulletspawnpoint not always player1’s bulletspawnpoint.
Set the player 2 spawnpoint in the inspector by dragging it in. The code with Find you have now is inefficient.
Also, a bullet is a gameObject and the rotation is transform.rotation and not Quaternion.identity.
If the spawn points is the child of the player, you can use GetComponentInChildren() and then go through the list (compare if the name is correct) until you find the correct Transform.
Do this in Awake() and store the transform in a class variable.
you can create custom tags and add them to your objects (like player1bulletspawn/player2bulletspawn) and you can FindGameObjectsWithTag(“”) which gives back a gameobject
Great! Now it will only shoot one fireball, but if I have two players online and press Q to shoot it. Both players will shoot the ball. How do I make it so only the player who presses the button, shoots it?