• 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
Question by biggamer7852 · Dec 06, 2020 at 05:11 PM · fpsgundamageshooterhealth

damage system

so i have a gun script and a custom bullet script does any one know how i can make a damage system like the enemy will have 100 health and not a one shot

this is the gun script //bullet force public float shootForce, upwardForce;

 //Gun stats
 public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
 public int magazineSize, bulletsPerTap;
 public bool allowButtonHold;

 int bulletsLeft, bulletsShot;

 //Recoil
 public Rigidbody playerRb;
 public float recoilForce;

 //bools
 bool shooting, readyToShoot, reloading;

 //Reference
 public Camera fpsCam;
 public Transform attackPoint;

 //Graphics
 public GameObject muzzleFlash;
 public TextMeshProUGUI ammunitionDisplay;

 //bug fixing :D
 public bool allowInvoke = true;

 private void Awake()
 {
     //make sure magazine is full
     bulletsLeft = magazineSize;
     readyToShoot = true;
 }

 private void Update()
 {
     MyInput();

     //Set ammo display, if it exists :D
     if (ammunitionDisplay != null)
         ammunitionDisplay.SetText(bulletsLeft / bulletsPerTap + " / " + magazineSize / bulletsPerTap);
 }
 private void MyInput()
 {
     //Check if allowed to hold down button and take corresponding input
     if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
     else shooting = Input.GetKeyDown(KeyCode.Mouse0);

     //Reloading 
     if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
     //Reload automatically when trying to shoot without ammo
     if (readyToShoot && shooting && !reloading && bulletsLeft <= 0) Reload();

     //Shooting
     if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
     {
         //Set bullets shot to 0
         bulletsShot = 0;

         Shoot();
     }
 }

 private void Shoot()
 {
     readyToShoot = false;

     //Find the exact hit position using a raycast
     Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); //Just a ray through the middle of your current view
     RaycastHit hit;

     //check if ray hits something
     Vector3 targetPoint;
     if (Physics.Raycast(ray, out hit))
         targetPoint = hit.point;
     else
         targetPoint = ray.GetPoint(75); //Just a point far away from the player

     //Calculate direction from attackPoint to targetPoint
     Vector3 directionWithoutSpread = targetPoint - attackPoint.position;

     //Calculate spread
     float x = Random.Range(-spread, spread);
     float y = Random.Range(-spread, spread);

     //Calculate new direction with spread
     Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, 0); //Just add spread to last direction

     //Instantiate bullet/projectile
     GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity); //store instantiated bullet in currentBullet
     //Rotate bullet to shoot direction
     currentBullet.transform.forward = directionWithSpread.normalized;

     //Add forces to bullet
     currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
     currentBullet.GetComponent<Rigidbody>().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);

     //Instantiate muzzle flash, if you have one
     if (muzzleFlash != null)
         Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);

     bulletsLeft--;
     bulletsShot++;

     //Invoke resetShot function (if not already invoked), with your timeBetweenShooting
     if (allowInvoke)
     {
         Invoke("ResetShot", timeBetweenShooting);
         allowInvoke = false;

         //Add recoil to player (should only be called once)
         playerRb.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse);
     }

     //if more than one bulletsPerTap make sure to repeat shoot function
     if (bulletsShot < bulletsPerTap && bulletsLeft > 0)
         Invoke("Shoot", timeBetweenShots);
 }
 private void ResetShot()
 {
     //Allow shooting and invoking again
     readyToShoot = true;
     allowInvoke = true;
 }

 private void Reload()
 {
     reloading = true;
     Invoke("ReloadFinished", reloadTime); //Invoke ReloadFinished function with your reloadTime as delay
 }
 private void ReloadFinished()
 {
     //Fill magazine
     bulletsLeft = magazineSize;
     reloading = false;
 }

}

this is the custom bullet public class CustomBullet : MonoBehaviour { //Assignables public Rigidbody rb; public GameObject explosion; public LayerMask whatIsEnemies;

 //Stats
 [Range(0f,1f)]
 public float bounciness;
 public bool useGravity;

 //Damage
 public int explosionDamage;
 public float explosionRange;
 public float explosionForce;

 //Lifetime
 public int maxCollisions;
 public float maxLifetime;
 public bool explodeOnTouch = true;

 int collisions;
 PhysicMaterial physics_mat;

 private void Start()
 {
     Setup();
 }

 private void Update()
 {
     //When to explode:
     if (collisions > maxCollisions) Explode();

     //Count down lifetime
     maxLifetime -= Time.deltaTime;
     if (maxLifetime <= 0) Explode();
 }

 private void Explode()
 {
     //Instantiate explosion
     if (explosion != null) Instantiate(explosion, transform.position, Quaternion.identity);

     //Check for enemies 
     Collider[] enemies = Physics.OverlapSphere(transform.position, explosionRange, whatIsEnemies);
     for (int i = 0; i < enemies.Length; i++)
     {
         //Get component of enemy and call Take Damage

         //Just an example!
         ///enemies[i].GetComponent<ShootingAi>().TakeDamage(explosionDamage);

         //Add explosion force (if enemy has a rigidbody)
         if (enemies[i].GetComponent<Rigidbody>())
             enemies[i].GetComponent<Rigidbody>().AddExplosionForce(explosionForce, transform.position, explosionRange);
     }

     //Add a little delay, just to make sure everything works fine
     Invoke("Delay", 0.05f);
 }
 private void Delay()
 {
     Destroy(gameObject);
 }

 private void OnCollisionEnter(Collision collision)
 {
     //Don't count collisions with other bullets
     if (collision.collider.CompareTag("Bullet")) return;

     //Count up collisions
     collisions++;

     //Explode if bullet hits an enemy directly and explodeOnTouch is activated
     if (collision.collider.CompareTag("Enemy") && explodeOnTouch) Explode();
 }

 private void Setup()
 {
     //Create a new Physic material
     physics_mat = new PhysicMaterial();
     physics_mat.bounciness = bounciness;
     physics_mat.frictionCombine = PhysicMaterialCombine.Minimum;
     physics_mat.bounceCombine = PhysicMaterialCombine.Maximum;
     //Assign material to collider
     GetComponent<SphereCollider>().material = physics_mat;

     //Set gravity
     rb.useGravity = useGravity;
 }

 /// Just to visualize the explosion range
 private void OnDrawGizmosSelected()
 {
     Gizmos.color = Color.red;
     Gizmos.DrawWireSphere(transform.position, explosionRange);
 }

}

thanks for your time.

Comment

People who like this

0 Show 1
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
avatar image sacredgeometry · Dec 06, 2020 at 05:27 PM 0
Share

Before trying to add more. Have you thought about refactoring your code to make it a little tidier? It may make it easier to work with and implement new features.

For example there is a lot of redundant code in here, there is also a lot of noise. As your code base grows, if it is written like this its going to get harder and harder to work with.


Another thing to mention is that your code should be self documenting:

 //Assign material to collider
      GetComponent<SphereCollider>().material = physics_mat;
 //Set gravity
      rb.useGravity = useGravity;



These comments are tautological. If your comments are just repeating what the code literally says then they are not useful. If your code isn't legible enough to be read without comments for basic things then your code isn't readable enough.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

188 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

Related Questions

FPS damage arrow? (raycast shoot) 2 Answers

how to take health/hearth from player when colliding with an object? 2 Answers

FPS TUTORIAL: AI robot not taking damage. 0 Answers

Cant Kill Multiplayer Player In Unity, Help! 0 Answers

How to Implement First Shot Accuracy? 2 Answers


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