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 sinkler747 · Feb 19, 2013 at 01:24 AM · enemy damageheadshot

head shots

i'm trying to script damage to enemy. this enemy has a capsule collider up to his neck, and then i have a sphere collider covering his head. i've tried several different ways, but none seem to work.everything works up until the headshot. i tagged the sphere as a HeadCollider. if there are any suggestions or others ways to do it please let me know.

 #pragma strict
 
 var range: float = 1000;
 var force: float = 1000;
 var clip : int   = 20;
 var bulletsPerClip:int  = 60;
 var reloadTime :float  = 3.3;
 var bulletsLeft: int = 0;
 var shootTimer: float = 0;
 var shootCooler: float = 0.9;
 var shootAudio: AudioClip;
 var reloadAudio: AudioClip;
 var damage: int = 10;  
 var instantDeath: int = 100;
 
 
 
 
 
 function Start () 
 {
 
     bulletsLeft = bulletsPerClip;
 
 
 }
 
 function Update () 
 {
     if(shootTimer > 0)
     {
         shootTimer -= Time.deltaTime;
     }
     
     if(shootTimer < 0)
     {
         shootTimer = 0;
     }
     
     if(Input.GetMouseButton(0) && bulletsLeft)
     {
         if(shootTimer == 0)
         {
             
         
         PlayShootAudio();
         RayShoot();
         
         shootTimer = shootCooler;
         
         }
     }
 }
 
 function RayShoot()
 {
     var hit: RaycastHit;
     var directionRay = transform.TransformDirection(Vector3.forward);
     Debug.DrawRay(transform.position, directionRay * range, Color.red);
     
     
     if(Physics.Raycast(transform.position, directionRay, hit, range))
     {
         if(hit.rigidbody)
         {
             hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
             hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
         }
         else if(hit.collider == GameObject.FindWithTag ("HeadCollider"))
                 {
                      Destroy(gameObject);
                     //hit.collider.SendMessageUpwards("ApplyDamage", instantDeath, SendMessageOptions.DontRequireReceiver);
                 }
         
     }
     
 //    if(Physics.Raycast(transform.position, directionRay, hit, range))
 //    {
 //        
 //    }
     
     
     bulletsLeft --;
     if(bulletsLeft < 0)
     {
         bulletsLeft = 0;
         clip --;
     }
         
         if(bulletsLeft == 0)
         {
             Reload();
         }
     
     
 }
 
 function Reload()
 {
     PlayReloadAudio();
     yield WaitForSeconds(reloadTime);
     
     if(clip >0)
     {
         bulletsLeft = bulletsPerClip;
     }
 
 
 }
 
 function PlayShootAudio()
 {
     audio.PlayOneShot(shootAudio);
 
 }
 
 
 function PlayReloadAudio()
 {
     audio.PlayOneShot(reloadAudio);
 
 }
Comment
Add comment · 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 sinkler747 · Feb 21, 2013 at 04:50 AM 0
Share

I could not get that to work for me. I worked on it during some spare time, and eventually thought to Debug it, so i took out the Destroy(gameObject); and inserted a Debug.Log("Shot to the Head"); --- when i tested it, it recognized the hit, but only one time.. it would never come again..... now i do have a cross hair script that i used the same Debug on and it recorded the hit every time without fail. so...I don't know why . i will continue to research it, but if you have any suggestions that would be wonderful

1 Reply

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

Answer by ByteSheep · Feb 19, 2013 at 02:18 AM

On line 69 in your code you have:

else if(hit.collider == GameObject.FindWithTag ("HeadCollider"))

I tested this and the if statement will never return true since the two arguments will never be equal.. However if you make sure they are of the same type e.g. a string then it does work. Here is an example:

else if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)

This worked for me so I hope you will be able to achieve the same results :) The statement is basically checking if the gameobject that was hit has the same name as the object that was tagged with the "HeadCollider" tag.

Comment
Add comment · Show 1 · 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 sinkler747 · Feb 25, 2013 at 04:03 AM 0
Share

after going back and forth with different ideas, i came back to this one, and i got it to work. here is the code

 var range: float = 1000;
 var force: float = 1000;
 var clip : int   = 20;
 var bulletsPerClip:int  = 60;
 var reloadTime :float  = 3.3;
 var bulletsLeft: int = 0;
 var shootTimer: float = 0;
 var shootCooler: float = 0.9;
 var shootAudio: AudioClip;
 var reloadAudio: AudioClip;
 var damage: int = 50;  
 
 
 
 
 
 
 function Start () 
 {
 
     bulletsLeft = bulletsPerClip;
 
 
 }
 
 function Update () 
 {
     if(shootTimer > 0)
     {
         shootTimer -= Time.deltaTime;
     }
     
     if(shootTimer < 0)
     {
         shootTimer = 0;
     }
     
     if(Input.Get$$anonymous$$ouseButton(0) && bulletsLeft)
     {
         if(shootTimer == 0)
         {
             
         
         PlayShootAudio();
         RayShoot();
         
         shootTimer = shootCooler;
         
         }
     }
 }
 
 function RayShoot()
 {
     var hit: RaycastHit;
     var directionRay = transform.TransformDirection(Vector3.forward);
     Debug.DrawRay(transform.position, directionRay * range, Color.red);
     
     
     if(Physics.Raycast(transform.position, directionRay, hit, range))
     {
         if(hit.collider.gameObject.name == GameObject.FindWithTag("Body").gameObject.name)
         //if(gameObject.FindWithTag("Body"))
         {
             Debug.Log("I hit the body");
             //hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
             //hit.collider.Send$$anonymous$$essageUpwards("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
          //else if(gameObject.CompareTag("HeadCollider"))
         else if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)
         {
             Debug.Log("That was a headshot");
         }
         
     }
     
 //    var hit: RaycastHit;
 //        //var directionRay = transform.forward;
 //    var directionRay = transform.TransformDirection(Vector3.forward);
 //    Debug.DrawRay(transform.position, directionRay * range, Color.red);
 //    
 //    if(Physics.Raycast(transform.position, directionRay, hit, range))
 //    {
 //        if(gameObject == gameObject.FindWithTag("Body"))
 //        {
 //            //hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
 //            //hit.collider.Send$$anonymous$$essageUpwards("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
 //            Debug.Log("bodyshot");
 //        }
 //        else if(gameObject == gameObject.FindWithTag("HeadCollider"))
 //        {
 //            Debug.Log("That was a headshot");
 //        }
         
     //}
     // if(Physics.Raycast(transform.position, directionRay, hit, range))
     // {
         // if(hit.collider == gameObject.FindWithTag("HeadCollider"))
         // {
             // Debug.Log("That was a headshot");
         // }
     // }
     bulletsLeft --;
     if(bulletsLeft < 0)
     {
         bulletsLeft = 0;
         clip --;
     }
         
         if(bulletsLeft == 0)
         {
             Reload();
         }
     
     
 }
 
 function Reload()
 {
     PlayReloadAudio();
     yield WaitForSeconds(reloadTime);
     
     if(clip >0)
     {
         bulletsLeft = bulletsPerClip;
     }
 
 
 }
 
 function PlayShootAudio()
 {
     audio.PlayOneShot(shootAudio);
 
 }
 
 
 function PlayReloadAudio()
 {
     audio.PlayOneShot(reloadAudio);
 
 }

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

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers

How to import the object from server to unity 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