- Home /
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;
}
}
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
those are all for offline games this health script connects to photon so its alot differant
I've never used photons, so I'm sorry, but I can't help you. :(
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.