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 Nooke · Feb 19, 2017 at 11:00 PM · networkclientspawning problems

Spawning object at wrong location

Hi, I'm trying to have an object be spawned where the player is looking at on a key press. When the host presses the spawn key, everything works fine. The clients on the other hand have their objects spawned at coordinates (0,0,0) instead of where the player is looking at. Here is the code:

 public class PlaceGuard : NetworkBehaviour
 {
 
     private Camera MainCamera;
     private int layerMask = 1 << 8;
     public GameObject TrapPrefab;
     [SyncVar]
     private Vector3 HitLocation;
 
 
     void Start()
     {
         MainCamera = GetComponentInChildren<Camera>();
     }
 
     void Update()
     {
             CastRay();   
     }
     void CastRay()
     {
         //Spawning BearTrap
         if (Input.GetKeyDown(KeyCode.T))
         {
             Ray ray = MainCamera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
             RaycastHit hit;
             if (Physics.Raycast(ray.origin, ray.direction, out hit, 3.0F, layerMask))
             {
                 HitLocation = hit.point;
                 CmdSpawnTrap();
             }
         }
     }
     [Command]
     void CmdSpawnTrap()
     {
         GameObject Trap = (GameObject)Instantiate(TrapPrefab, HitLocation, Quaternion.identity);
         NetworkServer.SpawnWithClientAuthority(Trap, connectionToClient);
     }
 }
 

I'd appreciate any help.

Comment
Add comment · Show 3
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 Nooke · Feb 20, 2017 at 10:41 AM 0
Share

Anybody help please?

avatar image jwulf Nooke · Feb 20, 2017 at 03:25 PM 0
Share

Is it possible that you need to wait one frame with CmdSpawnTrap(), because HitLocation's value will only be synched at the end of the frame (as described in https://docs.unity3d.com/ScriptReference/Networking.SyncVarAttribute.html)?

But it is just an ide; I have no experience with unity network program$$anonymous$$g, so it is absolutely possible that [Command] already has that in $$anonymous$$d and the execution on the client is delayed accordingly.

avatar image Nooke jwulf · Feb 20, 2017 at 03:59 PM 0
Share

I tried setting up a coroutine to wait for a frame before calling the CmdSpawnTrap funtion, unfortunately that didn't work, the result is still the same. Any other ideas?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Nooke · Feb 25, 2017 at 05:13 PM

After alot of tial and error I finally found my mistake, I'll post the answer if someone encounters the same problem:

The solution was to add the Vector3 HitLocation variable as a parameter of the CmdSpawnTrap funtion.

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
avatar image
0

Answer by RensDevolp · Feb 21, 2017 at 09:49 AM

I think you have to use Network.instantiate (Network.Instantatiate) As far as I know, this syncs its position with other ones. Also make sure you have an RPC attached to the trap (at least that is how it works with photon)

Comment
Add comment · Show 3 · 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
avatar image Nooke · Feb 21, 2017 at 11:41 AM 0
Share

Isn't that what the code at line 37 does? When you say RPC do you mean a network identity component? Or a command/ClientRpc attribute to be put somewhere in my code?

avatar image RensDevolp · Feb 21, 2017 at 11:52 AM 0
Share

With photon you have a script called "PhotonRPC". If you put the transform as the observed component, it will sync with the rest of the players

avatar image Nooke RensDevolp · Feb 21, 2017 at 12:13 PM 0
Share

yeah that sounds like the network Transform in unet, I do have that component attached to my trap though.

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

6 People are following this question.

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

Related Questions

Why won't my network code run on LAN? 2 Answers

OnConnectedToServer not called when testing on same machine 0 Answers

Trouble with Server Client connection 1 Answer

Is there any difference calling Network.Disconnect or Network.CloseConnection from client? 1 Answer

Network.Instantiate fired from client AND server 1 Answer


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