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 PvTGreg · Jul 02, 2014 at 08:45 AM · networkingphoton

how do i apply damage to objects other that the player

hi so ive been following quill18's tutorial son networking and i was wondering how i can get the damage to work on other objects i think that i need to write another bit similer to this is this right or am i wrong?? any ideas?? also any idea how i can add a range onto the raycast?

 Health h = hitTransform.GetComponent<Health>();
 
             while(h == null && hitTransform.parent) {
                 hitTransform = hitTransform.parent;
                 h = hitTransform.GetComponent<Health>();
             }
 
             // Once we reach here, hitTransform may not be the hitTransform we started with!
 
             if(h != null) {
                 // This next line is the equivalent of calling:
                 //                    h.TakeDamage( damage );
                 // Except more "networky"
                 PhotonView pv = h.GetComponent<PhotonView>();
                 if(pv==null) {
                     Debug.LogError("Freak out!");
                 }
                 else {
                         h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage);
                 }
 
             }



but i will switch it health for the new script i created that manages the objects health

 using UnityEngine;
 using System.Collections;
 
 public class ObjectDestroy : MonoBehaviour {
     
     public float hitPoints = 100f;
     float currentHitPoints;
     
     // Use this for initialization
     void Start () {
         currentHitPoints = hitPoints;
     }
     
     [RPC]
     public void TakeDamage(float amt) {
         currentHitPoints -= amt;
         
         if(currentHitPoints <= 0) {
             Die();
         }
     }
 
     
     void Die() {
 
         if( GetComponent<PhotonView>().instantiationId==0 ) {
             Destroy(gameObject);
         }
 
     }
 }
 



and this is the full player shooting script so you can see

 using UnityEngine;
 using System.Collections;
 
 public class PlayerShooting : MonoBehaviour {
 
     float cooldown = 0;
     WeaponData weaponData;
 
     void Start() {
     
     }
 
     // Update is called once per frame
     void Update () {
         cooldown -= Time.deltaTime;
 
         if(Input.GetButton("Fire1")) {
             // Player wants to shoot...so. Shoot.
             Fire ();
         }
 
     }
 
     void Fire() {
         if(weaponData==null) {
             weaponData = gameObject.GetComponentInChildren<WeaponData>();
             if(weaponData==null) {
                 Debug.LogError("Did not find any WeaponData in our children!");
                 return;
             }
         }
 
         if(cooldown > 0) {
             return;
         }
 
         Debug.Log ("Firing our gun!");
 
         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
 
         Transform hitTransform;
         Vector3   hitPoint;
 
         hitTransform = FindClosestHitObject(ray, out hitPoint);
 
         if(hitTransform != null) {
             Debug.Log ("We hit: " + hitTransform.name);
 
             // We could do a special effect at the hit location
             // DoRicochetEffectAt( hitPoint );
 
             Health h = hitTransform.GetComponent<Health>();
 
             while(h == null && hitTransform.parent) {
                 hitTransform = hitTransform.parent;
                 h = hitTransform.GetComponent<Health>();
             }
 
             // Once we reach here, hitTransform may not be the hitTransform we started with!
 
             if(h != null) {
                 // This next line is the equivalent of calling:
                 //                    h.TakeDamage( damage );
                 // Except more "networky"
                 PhotonView pv = h.GetComponent<PhotonView>();
                 if(pv==null) {
                     Debug.LogError("Freak out!");
                 }
                 else {
                         h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage);
                 }
 
             }
 
 
         }
 
 
         cooldown = weaponData.fireRate;
     }
 
 
 
 
     Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {
 
         RaycastHit[] hits = Physics.RaycastAll(ray);
 
         Transform closestHit = null;
         float distance = 0;
         hitPoint = Vector3.zero;
 
         foreach(RaycastHit hit in hits) {
             if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
                 // We have hit something that is:
                 // a) not us
                 // b) the first thing we hit (that is not us)
                 // c) or, if not b, is at least closer than the previous closest thing
 
                 closestHit = hit.transform;
                 distance = hit.distance;
                 hitPoint = hit.point;
             }
         }
 
         // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
 
         return closestHit;
 
     }
 }
 

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
0

Answer by JusticeAShearing · Jul 05, 2014 at 03:54 PM

Well, I have some Unity tutorials that you may find useful. I hope that they cover all of the necessary topics. If not, they are part of a playlist that covers just about everything, so please do check the rest of it.

Player Firing: https://www.youtube.com/watch?v=TOweXqxchJU&list=PLB4DA4F8BDC3B82EE∈dex=5

Timed Destroy: https://www.youtube.com/watch?v=sVgwFndZqCY&list=PLB4DA4F8BDC3B82EE∈dex=6

Killing Enemies: https://www.youtube.com/watch?v=0ggje3orOJ8&list=PLB4DA4F8BDC3B82EE∈dex=8

Enemy Health: https://www.youtube.com/watch?v=2vIsIctVXAY&list=PLB4DA4F8BDC3B82EE∈dex=18

Comment
Add comment · Show 4 · 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 PvTGreg · Jul 05, 2014 at 06:56 PM 0
Share

those are all for offline games this health script connects to photon so its alot differant

avatar image JusticeAShearing · Jul 07, 2014 at 03:50 PM 0
Share

I've never used photons, so I'm sorry, but I can't help you. :(

avatar image PvTGreg · Jul 07, 2014 at 09:30 PM 0
Share

so why did you answer a question on photon networking?

avatar image JusticeAShearing · Jul 08, 2014 at 03:51 PM 0
Share

Firstly, the closest thing that I saw to it mentioning photon networking was 'networking'. That didn't say 'photon', or show any obvious pro$$anonymous$$ence in the question, so when I ran my Unity executable file and thought 'what does PvTGreg want to know', I thought about health, firing and it being on other objects. Therefore, due to its lack of obviousness, it was overlooked. Call me unintelligent, but summarizing a question is my most efficient way of answering its required points. Besides, the spelling mistake right next to 'networking' caused me to pay less attention to that part. If it was of importance, I immediately think that one would try harder to emphasize it, let alone allow errors in the same sentence, which is why I pay less attention to parts with spelling or grammar errors in.

Secondly, I couldn't imagine any real difference between the scripting, as I was convinced that it was all relative to the character, not the fact that it was involved with networking.

Thirdly, I wanted to help, so that's why I actually posted what I had in $$anonymous$$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

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

Related Questions

[Photon]Player vs. Player Collision 0 Answers

Correct way to design turn based multiplayer using Photon Unity Networking. 0 Answers

Photon Cloud max players 1 Answer

Client Disconnect with message “Peer Created” - PUN 0 Answers

Photon Unity Network - Refresh/Tickrate 2 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