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 oliver-jones · May 22, 2012 at 02:34 PM · networkrpcprojectilefire

Network - Firing Projectile RPC Issue?

Hello,

I'm trying to get my projectile to fire on my network game (being a client or server). And I don't actually know the best way to do this, I don't think I'm doing it correct at all.

Whats happening right now, is if I fire on the server, my character fires fine but the clients also fire, but in an upward direction (its top down). If I fire on the clients side, you see the projectile fire out of everyones gun for a split second, then it disappears.

I'm trying to get to grips with the RPC, I think I see one of my problem, but again, I don't even know if this is the correct way to do it.

This is my gun script:

 #pragma strict
 
 var muzzleShot : Transform;
 var paintballRound : Rigidbody;
 
 function Update () {
     if(GameObject.FindWithTag("Player")){
         
         if (Input.GetButtonDown("Fire1")) {
             
             if(networkView.isMine){
                 Fire();
             }
             else{
                 var viewID = Network.AllocateViewID();
                 networkView.RPC("SendFire", RPCMode.AllBuffered, viewID, muzzleShot.transform.position, muzzleShot.transform.rotation); //viewID
             }
         }
     }
 }
 
 function Fire(){
     var clone : Rigidbody;
     clone = Instantiate(paintballRound, muzzleShot.transform.position, muzzleShot.transform.rotation);
     //clone = Network.Instantiate(paintballRound, muzzleShot.transform.position, muzzleShot.transform.rotation, 0);
             
     Physics.IgnoreCollision(clone.collider, GameObject.FindWithTag("Player").collider);
     clone.velocity = transform.TransformDirection (Vector3.forward * 10);
 }
 
 
 @RPC
 function SendFire(viewID : NetworkViewID, location : Vector3, rotate : Quaternion){    
         
     var clone : Transform;
     clone = Instantiate(paintballRound, location, rotate.identity) as Transform;
     var nView : NetworkView;
     nView = clone.GetComponent(NetworkView);
     nView.viewID = viewID;
 }


So whats it saying it, if the network is mine (I'm the player), fire a normal projectile NOT using the network, else if you are a client (viewing the person shooting), then also fire a projectile < this is why I believe the projectiles fire upwards on the client, because it isn't spawning it at the tip of the players gun, nor shooting in the right direction.

Please help me out, I've been stuck on this for days. Once I get this, I'll be able to get on my way!

Thanks all.

Comment
Add comment · Show 2
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 oliver-jones · May 22, 2012 at 02:51 PM 0
Share

I'm closely following the NetworkExample, so: Network.Instantiate(playerPrefab, spawnPoints.transform.position, spawnPoints.transform.rotation, 0);

avatar image whydoidoit · May 22, 2012 at 02:54 PM 0
Share

Yeah sorry, I read the code again and deleted my first comment :)

1 Reply

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

Answer by whydoidoit · May 22, 2012 at 02:53 PM

The way that works at the moment, all of the other players fire the RPC!!

You want to put that SendFire code only in the networkView.isMine call - that way the player actually doing the firing then goes and tells the others to fire.

You also don't want to use AllBuffered as that also tells you to instantiate again. Try OthersBuffered if you also want to do your Fire() code.

So your fire code could look like this:

 function Update () {
    if(networkView.isMine)
    {
 
        if (Input.GetButtonDown("Fire1")) {
 
           Fire();
           var viewID = Network.AllocateViewID();
           networkView.RPC("SendFire", RPCMode.OthersBuffered, viewID, muzzleShot.transform.position, muzzleShot.transform.rotation); //viewID
          }
        
     }
 }

Update:

Oliver also made his bullets not use the networkViewID and had his code move them on the client independently - my guess is that both the networkView and the local bullet code were moving the same item causing problems.

Comment
Add comment · Show 11 · 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 oliver-jones · May 22, 2012 at 02:59 PM 0
Share

Okay, I tried this but now my ServerPlayer controls all the clients firing, and my client firing does fire, but only fires upward on the Z axis (i believe this is to do with the (muzzleShot. transform)

avatar image whydoidoit · May 22, 2012 at 03:04 PM 0
Share

Oh hang on - authorative server? The players are sending a message to tell the server that they want to fire?

avatar image whydoidoit · May 22, 2012 at 03:07 PM 0
Share

It's probably down to that GameObject.FindTag returning false on the clients? Not sure about what that's doing :)

I think you just need to use rotate, not rotate.identity

avatar image oliver-jones · May 22, 2012 at 03:08 PM 0
Share

I don't mean to be troublesome, but like I said, I'm new to all this. I don't think its a authorative server, how would I know?

avatar image oliver-jones · May 22, 2012 at 03:18 PM 0
Share

I changed the rotate.identity to rotate, getting there... But now if someone fires, all the players fire too?

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

RPC a lot of lags (Photon) 0 Answers

Unity3d Network - Cant See Connected Players 3 Answers

Really quick/easy question about RPCs 1 Answer

Getting rotation to work on client 1 Answer

Network RPC 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