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 /
This question was closed Jan 07, 2017 at 07:46 AM by krsingh_aman for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by krsingh_aman · Jan 05, 2017 at 06:10 PM · raycastshootinghealth-deduction

Unable to reduce health in my networked shooting script

Hi Everyone. I was working on a Networked project in which i have to shoot other players on the network. i am using two c# scripts one for shooting and other for health deductions.

ShootScript

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class Player_Shooting : NetworkBehaviour 
 {
     private int damage = 50;
     private float range = 50;
     [SerializeField] Transform camTransform;
     private RaycastHit hit;
 
 
 
     [Command]
     void CmdTellServerWhoWasShot(string name, int dmg)
     {
         GameObject identifier = GameObject.Find (name);
         identifier.GetComponent<Player_health> ().ReduceHealth (dmg);
     }
 
 
     void Update()
     {
         CheckIfshoot ();
     }
 
 
     void ShootHim()
     {
         if (Physics.Raycast(camTransform.TransformPoint(0,0,0.5f), camTransform.forward,out hit,range))
         {
             Debug.Log (hit.transform.tag);
             if (hit.collider.tag == "Player") 
             {
                 string uniqueID = hit.transform.name;
                 CmdTellServerWhoWasShot (uniqueID, damage);
             }
         }
     }
 
     void CheckIfshoot()
     {
         if (!isLocalPlayer) 
         {
             return;
         } 
 
         if (Input.GetButtonDown("Fire1"))
         {
             ShootHim();
         }
     }
 
 }

HealthScript

 using System.Collections;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 
 public class Player_health : NetworkBehaviour
 {
     [SyncVar (hook="IfHealthChanges")] private int pHealth=200;
 
     private Text healthText;
     void Start () 
     {
         healthText = GameObject.Find ("Health_text").GetComponent<Text> ();
         SetHealth ();    
     }
 
     void Update () 
     {
         
     }
 
     void SetHealth()  
     {
         if (isLocalPlayer) 
         {
             healthText.text ="Health : "+ pHealth.ToString ();
         }
     }
 
     public void ReduceHealth(int dam)
     {
         pHealth -= dam;
     }
 
     void IfHealthChanges(int health) //the Hook Function
     {
         pHealth = health;
         SetHealth ();
     }
 }

But, whenever i am shooting player remains constant at 200. I am not able to find the problem. Please Help

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

  • Sort: 
avatar image
1
Best Answer

Answer by LK84 · Jan 05, 2017 at 06:29 PM

I haven't look through your whole code, but this doesn't look right:

 void CmdTellServerWhoWasShot(string name, int dmg)

Try passing the NetworkInstanceId instead (unique identifier for all GameObject with NetworkIdentity attached on all clients and server):

 [Command]
      void CmdTellServerWhoWasShot(NetworkInstanceId Id, int dmg)
      {
          GameObject identifier =  NetworkServer.FindLocalObject(Id);
          identifier.GetComponent<Player_health> ().ReduceHealth (dmg);
      }

And call it like this: CmdTellServerWhoWasShot (hit.GetComponent<NetworkIdentity>().netId, damage); This presumes of course that the hit collider game object has a network identity

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 krsingh_aman · Jan 07, 2017 at 07:44 AM 0
Share

thanks @L$$anonymous$$84, your suggestion also works nicely.

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

FPS shooting 1 Answer

problem with scripting 1 Answer

Random Raycast inside crosshairs 1 Answer

Arrow not shooting the in right orientation 0 Answers

Problem in Shooting Accuracy 0 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