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 Wesusa · Sep 10, 2020 at 04:43 PM · unity 5fpsunityeditorfps controllerheadshot

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);
     }
 }

} ```

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

0 Replies

· Add your reply
  • Sort: 

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

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

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

Movement in Mid Air WITH a reduced speed 1 Answer


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