I’m making a fps multiplayer shooter an I’ve come across an issue. The player shoots the shot fine and the raycast is shot but the player that is hit doesn’t take damage. I’ve worked on this for days trying many answers found on other forums and questions but they didn’t work.
Weapons Scripts
void Fire () {
int id = Animator.StringToHash("Ani");
if (isAiming == true)
ani.SetInteger (id, 4);
else
ani.SetInteger (id, 5);
ammo -= 1;
isCooled = false;
Invoke ("MuzzleReset", .05f);
Invoke ("Cool", coolDownTime);
muzzleFlash.SetActive (true);
Debug.Log ("Fired");
ass.PlayOneShot (shoot);
RaycastHit hit;
if (isAiming == true) {
shotPos = Shooter.transform.position;
}
if (isAiming == false) {
shotPos = Camera.main.transform.position;
}
if (Physics.Raycast(shotPos, transform.forward ,out hit ,range))
Debug.DrawRay(shotPos, transform.forward, Color.green); // draw the debug;
if (hit.transform.tag == "Body") {
Player playerr = hit.transform.gameObject.GetComponentInParent<Player> ();
playerr.health -= damage;
}
hit.rigidbody.AddForceAtPosition (transform.forward * bulletForce, hit.point);
if (hit.transform.tag == "Hit") {
Debug.Log ("HIT!!!");
}
}
Player Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : NetworkBehaviour {
public int[] weaponInv;
[SyncVar]
public int currentWeapon;
public GameObject[] weapons;
public AudioClip failPickUp;
public AudioClip switchGun;
public Animator anim;
public AudioListener al;
public Camera cam;
public Text pickupText;
public Canvas canvas;
public Image healthBar;
[SyncVar]
public float health = 100;
float maxHealth =100;
float healthRatio;
public Sprite[] gunPics;
public Image[] slots;
AudioSource audio;
Vector3 shotPos;
KillPrint KillPrin;
void Start () {
audio = GetComponent<AudioSource> ();
if (isLocalPlayer == false) {
al.enabled = false;
cam.enabled = false;
canvas.enabled = false;
}
KillPrin = GameObject.FindGameObjectWithTag ("Killfeed").GetComponent<KillPrint> ();
}
void Update () {
healthRatio = health / maxHealth;
Vector3 bar = healthBar.rectTransform.localScale;
bar.x = healthRatio;
healthBar.rectTransform.localScale = bar;
slots[0].sprite = gunPics[weaponInv[0]];
slots[1].sprite = gunPics[weaponInv[1]];
slots[2].sprite = gunPics[weaponInv[2]];
slots[3].sprite = gunPics[weaponInv[3]];
slots[4].sprite = gunPics[weaponInv[4]];
This is only parts of the code because the whole script is to long. Please help thanks in advance!