Raycast Weapon not cause damage
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!
why u r not seeing this? 3 $$anonymous$$s ago. Do u have any problem?
Answer by AshwinTheGammer · Jan 14, 2018 at 08:42 AM
Remove this line from code
if (hit.transform.tag == "Body") {
Player playerr = hit.transform.gameObject.GetComponentInParent<Player> ();
playerr.health -= damage;
}
and add this
public float damage = 3f;
IEnumerator ApplyDamage()
{
//Reduce the enemy's health
//Does NOT travel up the heirarchy.
if (hit.transform.tag != friendlyTag)
{
if (hit.collider.gameObject.GetComponent<Player>())
{
hit.collider.gameObject.GetComponent<Player>().ApplyDamage(damage);
}
hit.collider.SendMessage(damageMethodName, damage, SendMessageOptions.DontRequireReceiver);
}
And change Player HealthScript
public float CurrentHP; // Current amount of Hp.
public float maxHP = 100; // Max amount of Hp.
public void ApplyDamage (float damage)
{
if (CurrentHP > 0 && damage > 0)
{
CurrentHP -= damage;
}
}
Don`t forget to add above code. Remove this
healthRatio = health / maxHealth;
Your answer
Follow this Question
Related Questions
Raycast over Photon 0 Answers
Client can damage host but not vice versa? 0 Answers
Disable Damage Raycast? 1 Answer
Help with photon being used to make a mulitplayer fps 0 Answers
FPS Raycasting vs Instantiate 0 Answers