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 agentperrys · Jun 06, 2013 at 12:23 PM · networkinggun

Network Shoot/health script

Ok I am Making a network fps in unity and i am having some problems I made a health script and a shoot script so basically when the player shoots the other player the player hit loses health but the problem is they dont lose any health.

shoot script:

     var damage : int = 50;
     var meleeaim : Transform;
     var ammo = 15;
     var clips = 10;
     
     function Update() {
     if (Input.GetButtonDown("Fire1")) {
     attackdamage();
     //    animation.Play("shoot");
      }
      if (ammo <= 0 && clips >= 1) {
          reload();
      }    
     //if (animation.isPlaying == false) {
     //  animation.CrossFade("gunidel");
     //  }
     //  if (Input.GetKey(KeyCode.LeftShift)) {
     //      animation.CrossFade("swordrun");
     //  }
     // if (Input.GetKeyUp(KeyCode.LeftShift)) {
     //     animation.CrossFade("gunidel");
     // }
     }
     function attackdamage() {
             var hit : RaycastHit;
         if (Physics.Raycast(meleeaim.transform.position, meleeaim.transform.TransformDirection(Vector3.forward), hit)) {
         hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
         }
     }
     
     function shoot() {
         ammo -= 1;
     }    
     
     function reload() {
              clips -= 1;
              ammo = 10;
     }
 

Health Script:

 var Health = 100;
 
 function ApplyDammage (damage : int)
 {
     Health -= damage;
     
     if(Health <= 0)
     {
         Dead();
     }
 }
 
 function Dead()
 {
     RespawnMenu.playerIsDead = true; 
     Debug.Log("Player Died");
 }

network script:

 var gameName : String = "fragnass_game_make_original";
 var player : Transform;
 var spawnobject : Transform;
 private var refreshing : boolean;
 private var hostData : HostData[];
 private var poswidth : float;
 private var posheight : float;
 private var width : float;
 private var height : float;
 function Start() {
     poswidth = Screen.width * 0.05;
     posheight = Screen.width * 0.05;
     width = Screen.width * 0.1;
     height = Screen.width * 0.1;
 }
 
 function startServer() {
      Network.InitializeServer(18, 25001, !Network.HavePublicAddress);
      MasterServer.RegisterHost(gameName, "FPS Shooter Game", "This Is A FPS" );
 }
 
 function spawnplayer() {
     Network.Instantiate(player, spawnobject.position, Quaternion.identity, 0);
 }
 
 function OnServerInitialized() {
 Debug.Log("Server is initalized");
 spawnplayer();
 }
 
 function OnConnectedToServer() {
     spawnplayer();
 }
 
 function OnMasterServerEvent(mse : MasterServerEvent) {
     if (mse == MasterServerEvent.RegistrationSucceeded)
     Debug.Log("Server Registrated");
 }
 
 function refreshhostlist() {
     MasterServer.RequestHostList(gameName);
     refreshing = true;
     
 }
 
 function Update() {
     if (refreshing) {
         if (MasterServer.PollHostList().Length > 0) {
         refreshing = false;
         Debug.Log(MasterServer.PollHostList().Length);
         hostData = MasterServer.PollHostList();
         }
     }
 }
 
 function OnGUI() {
 if (!Network.isClient && !Network.isServer) {
         if (GUI.Button(Rect(poswidth, posheight, width, height), "Start Server")) {
             Debug.Log("starting server");
             startServer();
         }
         if (GUI.Button(Rect(poswidth, posheight * 1.2 + height, width, height), "Refresh Hosts")) {
             Debug.Log("Refreshing");
             refreshhostlist();
         }
         if (hostData) {
             for(var i : int = 0; i
                 {
Comment
Add comment · Show 5
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 LeMoine · Jun 06, 2013 at 02:19 PM 0
Share

Ouch, maybe you could edit your post first and make it more readable.

avatar image Ben-Stoneman ♦♦ · Jun 06, 2013 at 02:56 PM 0
Share

$$anonymous$$uch Better :)

avatar image agentperrys · Jun 06, 2013 at 04:44 PM 0
Share

ya sorry about the messiness when i posted it it got all crunched together and i didn't know how to fix it

avatar image DryTear · Jun 06, 2013 at 07:22 PM 0
Share

you need to use RPC, if youre dealing with sending messages over the network

avatar image agentperrys · Jun 07, 2013 at 04:45 PM 0
Share

thanks for the help but i couldn't understand the rpc so i tryed the sendmessage again and i found the problem the ApplyDamage had 2 m's but the health only had 1 anyways when i shoot a object with the script on it it kills everyone anyone know how to fix this.

ps. wil i have to use RPC's for this and if so then how would i go about doing it.

0 Replies

· Add your reply
  • Sort: 

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

17 People are following this question.

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

C# How to have a gun select 1 Answer

Official Unity Networking example? 0 Answers

UNET Server not setting [SyncVar] 0 Answers

RPC isn't called on objects with another name, even though they have the same script. 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