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
2
Question by LexGear · Nov 24, 2012 at 08:47 AM · raycastweaponaccessinghits

Accessing Script that Raycast hit

Hello, I've been at this for hours. I've got a level with a bunch of zombies placed from a prefab. They run around and try and kill the player.

The player has a weapon that casts a ray when mouse is down, and if it hits a zombie, it is meant to access that zombies health script and change its life value.

The problem I'm having, is when the ray hits any zombie, all zombies take damage. I'm not sure where I'm going wrong. This is my weapon script:

    #pragma strict
     
     private var fireRate : float;
     private var nextFire : float;
     var hit : RaycastHit;
     var weaponDamage : float;
     
     //Weapon sound effects
     var shotgunFire : AudioClip;
     
     function Start()
     {
         //Set rate of fire
         fireRate = 2;
         weaponDamage = 1;
     }
     
     function Update()
     {
         if (Input.GetButton ("Fire1") && Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
             audio.PlayOneShot(shotgunFire);
             
             //Cast ray, if hit zombie, deal damage to selected zombie
             if (Physics.Raycast (transform.position, transform.forward, hit, 100))
             {
                 //This doesn't draw a line, I mustn't be using it correctly
                 Debug.DrawLine (transform.position, transform.forward);
                 
                 if (hit.collider.gameObject.tag == "zombie")
                 {
                     var enemyLifeScript = hit.collider.GetComponent(ZombieHealth);
                     if (enemyLifeScript)
                         enemyLifeScript.life -= weaponDamage;
                     
                     //Other Things I've tried:
                     //hit.collider.gameObject.GetComponent(ZombieHealth).life = hit.collider.gameObject.GetComponent(ZombieHealth).life - weaponDamage;
                     //ZombieHealth.life = ZombieHealth.life - weaponDamage;
                 }
             }
         }
     }
     
     @script RequireComponent(AudioSource)
Comment
Add comment · Show 6
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 Jozxyqk · Nov 24, 2012 at 12:51 PM 0
Share

First thing that came to $$anonymous$$d: ZombieHealth.life isn't somehow a static/global variable is it?

How are you visualizing/debugging the health value?

avatar image LexGear · Nov 26, 2012 at 08:18 AM 0
Share

Hi Jozxyqk, the life variable is a static variable, and it is modified from outside the ZombieHealth Script.

What do you mean by visualizing / debugging the health value?

avatar image Fattie · Nov 26, 2012 at 08:48 AM 0
Share

do not use static variables. check unityGE$$anonymous$$S.com for a basic intro to accessing variables from other scripts

avatar image BPPHarv · Nov 26, 2012 at 08:52 AM 0
Share

A static field identifies exactly one storage location to be shared by all instances of a given closed class type. Hejlsberg et al., p. 434

http://www.dotnetperls.com/static

avatar image LexGear · Nov 26, 2012 at 08:55 AM 0
Share

ahhh thank you guys.

Show more comments

2 Replies

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

Answer by LukaKotar · Nov 24, 2012 at 09:28 AM

Basically, how I would do this would be by placing a function inside of your zombie script that would inflict the damage, and the damage would be handled by a float parameter.

In your zombie health script, you can add something like this:

 function ApplyDamage (damage : float) {
 life -= damage;
 }

And to do damage you would call the function on the hit object, and tell it how much damage to inflict:

 if(hit.collider.tag == "zombie"){
 hit.gameObject.SendMessageUpwards("ApplyDamage", weaponDamage); //Tell the game object you've hit to initiate a function called "ApplyDamage", and set the 'damage' float parameter to a desired value, which would be weaponDamage
 }

Hope it works.

Comment
Add comment · Show 17 · 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 LexGear · Nov 26, 2012 at 08:29 AM 0
Share

Hi thanks for your help, just so I can get some better understanding of all of this. I've tried reading up on "Send$$anonymous$$essageUpwards", but I don't fully get it. Could you explain what that term means?

avatar image LukaKotar · Nov 26, 2012 at 08:51 AM 0
Share

Well, the Send$$anonymous$$essageUpwards() function basically calls a function on another object. First thing you type in it is a function name. So for example, if you would have a function called Example(), and would like to call it from another object, you could use Send$$anonymous$$essageUpwards("Example") to call the function. If it has at least one parameter, you'd have to tell them their values. For example, Example (parameter : float) is a function that contains a parameter that I named "parameter". If you do have any, you will need to set the values. To set the value upon calling the function with Send$$anonymous$$essageUpwards, you would as before, put the function name as string in the beginning, and then just put values of the same type the parameter is. Example: Send$$anonymous$$essageUpwards("Example", 2.5) . You can also set the values from variables.

avatar image LexGear · Nov 26, 2012 at 08:54 AM 0
Share

Ahhhh thats pretty awesome thank you. I've set it up and it all looks good, but I'm gettin an error: 'gameObject' is not a member of unity bla bla. this is what I wrote:

if(hit.collider.tag == "zombie") { hit.gameObject.Send$$anonymous$$essageUpwards("ApplyDamage", weaponDamage); }

avatar image LukaKotar · Nov 26, 2012 at 09:00 AM 0
Share

Oops, try 'transform' ins$$anonymous$$d of 'gameObject'.

hit.transform.Send$$anonymous$$essageUpwards("ApplyDamage", weaponDamage);

avatar image LexGear · Nov 26, 2012 at 09:05 AM 0
Share

What happens if there are two scripts with two functions both named the same and both called by "Send$$anonymous$$essageUpwards"?

Show more comments
avatar image
0

Answer by IT_Criminal · Nov 24, 2012 at 01:06 PM

 RaycastHit hit;
 if(Physics.Raycast(CurrentWeapon.BarrelGO.position,CurrentWeapon.BarrelGO.TransformDirection(Vector3.forward),hit,250.0))
 {
    hit.transform.SendMessage("ApplyDAmage",CurrentWeapon.damage);
 }

i use this ...

replace currentweapon with the class that you are using for your weapons and barrelGO with the bullet spawnpoint (make sure the bullet-spawnpoint gameobject is pointing with the blue arrow torwards the direction of the shooting)

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.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Need a script for a gun that shoots bullet using raycast 2 Answers

Weapon Customizations Script Problem 0 Answers

Not allowed create MonoBehaviour using new keyword 0 Answers

Raycast on Particle - Transform 0 Answers

GetComponent Picking up multiple scripts from differrent objects? 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