• 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 el-mas-pro-470 · Jan 14, 2019 at 09:28 PM · fpsenemydamageshooterindicator

FPS damage arrow? (raycast shoot)

Hello! how to i make the damage arrows depending the site of enemy shoot? thanks you! enemy shoot script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyShoot : MonoBehaviour {
     
     public float Damage;
 
     public Camera EnemyCam;
 
     public float Force;
 
     public GameObject HitEffect;
 
     public float Range;
 
     public float FireRate = 0f;
     
     private float NextTimeToFire = 0f;
 
     public float MaxAmmo;
     public float CurrentAmmo;
     public bool Reloading;
     public float ReloadTime;
     public float ReloadAmmo;
 
     public ParticleEmitter MuzzleFlash;
 
     public Transform LightPosition;
     public Transform Light;
     public AudioClip ShootSound;
     void Start () 
     {
         CurrentAmmo = MaxAmmo;
     }
     
     
     void Update ()
     {
         if(CurrentAmmo <= 0)
         {
             CurrentAmmo = 0;
             StartCoroutine(Reload());
         }
 
         if(Time.time >= NextTimeToFire)
         {
             NextTimeToFire = Time.time + 1f/FireRate;
                 Fire();
         }
     }
 
     IEnumerator Reload()
     {
         Reloading = true;
         yield return new WaitForSeconds(ReloadTime);
         CurrentAmmo = ReloadAmmo;
         Reloading = false;
     }
 
     void Fire()
     {
         if(CurrentAmmo > 0)
         {
             MuzzleFlash.Emit();
         CurrentAmmo--;
         audio.Play();
         audio.clip = ShootSound;
 
         Instantiate(Light, LightPosition.position, LightPosition.rotation);
 
         RaycastHit hit;
         if (Physics.Raycast(EnemyCam.transform.position, EnemyCam.transform.forward, out hit, Range))
         {
             Debug.Log(hit.transform.name);
             
             PlayerHealth target = hit.transform.GetComponent<PlayerHealth>();
             if(target != null)
             {        
                 target.TakeDamagePlayer(Damage);
             }
             
             if(hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * Force);
             }
             
             Instantiate(HitEffect, hit.point, Quaternion.LookRotation(hit.normal));
     }
 
 }
 }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by julio-hb · Jan 14, 2019 at 10:51 PM

as of what I understand, you want the arrow to do different damage based on where it hit? (like a headshot would deal more damage than a arrow to the knee?)

personally, I would set up different tags to check here I hit the character e.g:

arms,legs, torso, head ect. would have different colliders, and tags

then in line ca. 75 you would check which part this is hit, using hit.transform.tag.

heres some code that might be useful

 Transform target = hit.transform;
 if(target != null)
 {        
     switch(target.tag)
     {
         case "arm":
             target.GetComponent<PlayerHealth>().TakeDamagePlayer(arm_damage);
         break;
         
         case "leg":
             target.GetComponent<PlayerHealth>().TakeDamagePlayer(leg_damage);
         break;
         
         case "torso":
             target.GetComponent<PlayerHealth>().TakeDamagePlayer(torso_damage);
         break;
         
         case "head":
             target.GetComponent<PlayerHealth>().TakeDamagePlayer(head_damage);
         break;
             
     }
 
     
 }

kinda hardcoded, but you should get the point.

Comment
Add comment · Share
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
0

Answer by Cornelis-de-Jager · Jan 14, 2019 at 11:50 PM

@julio-hb has given and example of one type of Damage system. I would like to give an example of another that I tend to use. Its very simple and very scaleable (especially when you add armor/damage reduction)

The basic idea behind it is that each part of the body has its own collider, these objects will be children to the Player object. The Player object will have the health/stats/info about the player and is kept in a single game object that does not have a collider. When something hits each collider / When a body part gets hit, it calls a the appropriae function in the Player script.

Ok, thats the concept, now lets see an implementation - I have also added slow effect as well as damage effect to show how this system is versitile and flexible:

 /// <Summary>
 /// Basic player script
 /// </Summary>
 public class Player : MonoBehaviour {
 
     public int health;
     public float speed;
 
     void FixedUpdate () {
         // Do normal movement and stuff
     }
 
     ///////////////// DAMAGE //////////////////
     public void DamagePlayer (int damage) {
         health = health - damage > 0 : health - damage ? 0;
         if (health == 0)
             Die();
     }
 
     ///////////////// SLOW //////////////////
     public void SlowPlayer (float slow, float time) {
         StartCoroutine (ApplySlow (slow, time))
     }
 
     IEnumerator ApplySlow (float slow, float time) {
         
         var tempSlow = slow <= speed ? slow : speed;
         speed -= tempSlow;
         yield return new WaitForSeconds (time);
         speed += tempSlow;
     }
 
     ///////////////// FORCE //////////////////
     public void ApplyForce (Vector3 force) {
         GetComponent<RigidBody> ().AddForce(force);
     }
 
     ///////////////// SLOW //////////////////
     void Die() {
         // Handle Death
     }
 }
 



 /// <Summary>
 /// Script on Limbs
 /// </Summary>
 public class Limb : MonoBehaviour {
 
     public float armor;
     public Player player;
 
     void Start () {
         // Get the player
         player = transform.parent.GetComponent<Player>();
     }
 
 
     /// <Summary>
     /// Call this Directly when using Raycast.
     /// Otherwise let the OnCollision event call it
     /// </Summary>
     public void Hit (int damage = 0, float slow = 0, float slowTime = 0, Vector3 moveForce = null) {
         
         if (damage > 0){
             // Do damage reduction
             damage = damage < armor ? 0 : damage - armor;
             armor = damage < armor ? armor - damage : 0;
 
             // Deal the damage
             player.DamagePlayer(damage);
         }
 
         if (slow > 0 && slowTime > 0)
             player.SlowPlayer (slow, slowTime)
 
         if (moveBody != null)
             player.ApplyForce (moveForce);
         
     }
 }
 
 



 void Fire() {
     if(CurrentAmmo > 0)
     {
         MuzzleFlash.Emit();
         CurrentAmmo--;
         audio.Play();
         audio.clip = ShootSound;
  
         Instantiate(Light, LightPosition.position, LightPosition.rotation);
  
         RaycastHit hit;
         if (Physics.Raycast(EnemyCam.transform.position, EnemyCam.transform.forward, out hit, Range))
         {
             Debug.Log(hit.transform.name);
              
             Limb target = hit.transform.GetComponent<Limb>();
             if(target != null)
             {        
                 target.hit(damage:Damage, slow: Slow, slowTime: SlowTime, moveForce: -hit.normal * Force);
             }             
              
             Instantiate(HitEffect, hit.point, Quaternion.LookRotation(hit.normal));
         }
     }
  }
 



Comment
Add comment · Share
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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

135 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

Related Questions

Deal damage on collision 2 Answers

damage system 0 Answers

Enemy fire rate? help! 2 Answers

Enemy deals damage to player on contact 2 Answers

Compiler error FPSplayer script 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