Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 anwe · Nov 27, 2011 at 03:30 PM · multiplayernetworknetworkviewnetwork.instantiate

How to let the other network player to see a launched rocket and its explosion?

I made an helicopter game in which u can drive it. The problem is that when a player press the input fire the rocket and its explosion is only seen by the player who entered the input, while it's not seen by the other. Could someone help me, please?

PS here's the code

 function ShootMissiles(){
     var raymissiles : Ray =  camera.main.ScreenPointToRay(Vector3(Screen.width/2,Screen.height/2,0));
     for (var i in spawnMissili){
         yield WaitForSeconds(0.3);
         var missile = Instantiate(rocket, i.position, i.localRotation);
         missile.transform.localEulerAngles = transform.localEulerAngles;
         //missile.transform.Rotate(Vector3.up*90, Space.Self);
         missile.rigidbody.AddForce(transform.forward*250, ForceMode.Impulse);
         //Physics.IgnoreCollision(missile.collider, collider);
    
      }
 }
Comment
Add comment
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

1 Reply

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

Answer by syclamoth · Nov 27, 2011 at 11:02 PM

What do you know about network instantiation? It's not quite as straightforward as you think. Try turning the 'ShootMissiles' function into an RPC- split it up into two functions, like this-

 function ShootMissiles(){
     var raymissiles : Ray =  camera.main.ScreenPointToRay(Vector3(Screen.width/2,Screen.height/2,0));
     for (var i in spawnMissili){
         yield WaitForSeconds(0.3);
         if(networkView.isMine)
         {
             // This bit tells the network to set up a viewID for you to assign manually.
             var missileViewID : NetworkViewID = Network.AllocateViewID();
             // Here we distribute the signal to all players, with the viewID to sync the missile!
             networkView.RPC("SpawnMissile", RPCMode.All, i.position, i.localRotation, transform.forward*250);
         }
      }
 }

 @RPC
 function SpawnMissile(position : Vector3, rotation : Quaternion, force : Vector3, viewID : NetworkViewID)
 {
     var missile = Instantiate(rocket, position, rotation);
     missile.transform.localEulerAngles = transform.localEulerAngles;
     //missile.transform.Rotate(Vector3.up*90, Space.Self);
     missile.rigidbody.AddForce(force, ForceMode.Impulse);
     missile.networkView.viewID = viewID;
     //Physics.IgnoreCollision(missile.collider, collider);
 }


Now, having done this, we then need to add a few things to the missile to make it synchronise between players properly (since because the physics engine is somewhat unstable, you won't be guaranteed that it'll land at the same spot on every machine, especially at long range). To do this, we will make the player that fired the missile 'authoritative', and adjust the missile's flight-path to their local physics simulation if it deviates on other computers.

First, make sure that your missile prefab has a networkView component, then add a script that goes something like this-

 var remoteVelocity : Vector3;
 var remoteLocation : Vector3;
 var remoteRotation : Quaternion;

 function FixedUpdate()
 {
     if(networkView.isMine)
     {
         remoteVelocity = rigidbody.velocity;
         remoteLocation = rigidbody.position;
         remoteRotation = rigidbody.rotation;
     } else {
         rigidbody.velocity = remoteVelocity;
         rigidbody.position = remoteLocation;
         rigidbody.rotation = remoteRotation;
     }
 }

 function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
     stream.Serialize(remoteVelocity);
     stream.Serialize(remoteLocation);
     stream.Serialize(remoteRotation);
 }

Then, make sure that this script is the 'observed' value on the missile's networkView, and that the networkView is using 'Unreliable' synchronisation mode (since we expect the values to change every frame).

Comment
Add comment · Show 1 · 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 anwe · Nov 28, 2011 at 10:27 AM 0
Share

thank u so much !! :D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Does the position change need state synchronization? 2 Answers

[uNet] Need help Converting From Unity Network to uNet 0 Answers

How do you remove RPC's from the RPC buffer that where created on behalf of a player that is now disconnected with an authoritative server? 1 Answer

Network View - Player is blinking, and visible only partially during movement. 0 Answers

Sending info to/from different networkViews 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