Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Goldiedog123 · Mar 05, 2017 at 03:25 PM · c#networkinginputmultiplayershooting

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!!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ExtinctSpecie · Mar 07, 2017 at 10:30 AM 0
Share

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);

1 Reply

· Add your reply
  • Sort: 
avatar image
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.
  
              }
          }
      }
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges