- Home /
Spawning objects in multiplayer game
HELLO FELLOW UNITY LOVERS!
Okay my problem is: I have made a script that when a player hits the input "Earth 2" it spawns a wall (Looks like a rock, made it in blender).
When the wall spawns it spawns in front of the player who used the input... but it also spawns the wall in front of the other players who didn't use the input "Earth2" . How do I make it only spawn infront of the player who used it?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class earthwall : MonoBehaviour
{
public GameObject Earthwall;
private float fireStart = 0f;
private float fireCooldown = 0.1f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
return;
}
if (Input.GetButton("Earth2"))
{
if (Time.time > fireStart + fireCooldown)
{
fireStart = Time.time;
GameObject InstantiatedProjectile = (GameObject)Network.Instantiate(Earthwall, transform.position, transform.rotation, 0);
}
}
}
}
ALSO: isLocalPlayer is an error??
THANKS IN ADVANCE!!
so i guess the same script is attached to player1 and player2 and you have 2 buttons with the same name "Earth2" if you hit the Earth2 button it will spawn the Earthwall object on both players that it right because the script will trigger in both players what you can do is change the way this trigger happens one option is to make 2 different buttons another option is to check the parent of the Earth2 button
if(Input.GetButton("Earth2") && transform.parent.name == "player1") GameObject InstantiatedProjectile = (GameObject)Network.Instantiate(Earthwall, transform.parent.transform.potision , transform.parent.transform.rotation, 0);
Answer by crare · Nov 21, 2017 at 02:06 PM
isLocalPlayer comes from namespace called UnityEngine.Networking that you have to include in the star and you need to change your MonoBehaviour to NetWorkBehaviour. Also just instantiating your object is not enough to show up in multiplayer for others. use Network.spawn(gameobject); after instantiating it. You also may have to add NetworkIdentity - object to your gameobject in the inspector. Aand you're talking about projectile that needs to be moving so you may need NetworkTransform to update it's movement or you could give it certain parameters to move in certain way, when it is spawned/instantiated. = that projectile-gameobject's script Start()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class earthwall : NetworkBehaviour
{
public GameObject Earthwall;
private float fireStart = 0f;
private float fireCooldown = 0.1f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
return;
}
if (Input.GetButton("Earth2"))
{
if (Time.time > fireStart + fireCooldown)
{
fireStart = Time.time;
GameObject InstantiatedProjectile =
(GameObject)Network.Instantiate(Earthwall, transform.position, transform.rotation, 0);
Network.spawn(InstantiatedProjectile); // spawn the object for other players to see it.
}
}
}
}
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Detecting when the other player presses a button 1 Answer
Multiplayer server controls all players 0 Answers
Multiplayer - Information needed. 1 Answer
How to synchronize GameObjects 2 Answers