- Home /
Shooting does not affect the Health
I followed a tutorial in which some scripts were partially finished. After finishing the tutorial, I elaborated on much of it, adding platforms, and an enemy. I tried to add a headshot mechanic to the enemy by making a sphere collider the child of the enemy's prefab. I gave the sphere collider a tag called "Dummyhead." This is the script for the raycast shooting. The "ShootableBox" was a box that was already part of the tutorial. You can see I tagged it with "Shootable" and the reason I'm puzzled the headshot doesn't work because the rigidbody reacts for both the shootablebox and the enemy. The shootablebox's health works perfectly fine.
```using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RaycastShoot : MonoBehaviour { public int gunDamage = 1; public int headshot = 3; public float fireRate = .25f; public float weaponRange = 50f; //how far it can shoot, 50 units in this case public float hitForce = 100f; public Transform gunEnd;
private Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(.07f); //the waitforseconds duration is how long the laser will be visible after it has been fired, in this case it is 0.7 seconds.
private AudioSource gunAudio; //audio source called gunAudio
private LineRenderer laserLine; //renders a line
private float nextFire;
// Start is called before the first frame update
void Start()
{
laserLine = GetComponent<LineRenderer>();
gunAudio = GetComponent<AudioSource>();
fpsCam = GetComponentInParent<Camera>(); //get and store a reference to our camera, getcomponent in parent will check the first object it is associated to
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1") && Time.time > nextFire) //checks everytime a player attempts to fire if enough time has apssed since the last fire.
{
nextFire = Time.time + fireRate;
StartCoroutine(ShotEffect());
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
laserLine.SetPosition(0, gunEnd.position);
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
// Set the end position for our laser line
laserLine.SetPosition(1, hit.point);
// Get a reference to a health script attached to the collider we hit
if (hit.collider.tag == "Shootable") //checks if it hit the dumb box that was put into here by default
{
ShootableBox health = hit.collider.GetComponent<ShootableBox>();
// If there was a health script attached
if (health != null)
{
// Call the damage function of that script, passing in our gunDamage variable
health.Damage(gunDamage);
}
// Check if the object we hit has a rigidbody attached
if (hit.rigidbody != null)
{
// Add force to the rigidbody we hit, in the direction from which it was hit
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
}
else //refering the shootable box tag
{
if (hit.collider.tag == "Dummyhead")
{
HealthScript currentHealth = hit.collider.GetComponent<HealthScript>();
if (currentHealth != null)
{
currentHealth.Damage(headshot);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
} //Dummyhead script ends here
}
}
}
}
private IEnumerator ShotEffect()
{
gunAudio.Play();
laserLine.enabled = true;
yield return shotDuration;
laserLine.enabled = false;
}
}
```
This is the script for the shootablebox:
``` using UnityEngine; using System.Collections;
public class ShootableBox : MonoBehaviour {
//The box's current health point total
public int currentHealth = 3;
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//if health has fallen below zero, deactivate it
gameObject.SetActive (false);
}
}
} ```
This is the script for the Healthscript of the enemy: ``` using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HealthScript : MonoBehaviour { public int currentHealth = 3;
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//if health has fallen below zero, deactivate it
gameObject.SetActive (false);
}
}
} ```
Your answer
Follow this Question
Related Questions
I want to add mobile controls in Horror FPS kit 0 Answers
FPS 3D camera view problem 0 Answers
WebGl Mouse Look Problem 0 Answers
MY FPSE`S fps is not moving need help! I tried all solutions! 0 Answers