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 crowley91t · Apr 03, 2013 at 08:30 PM · javascriptgameobjectraycastdamageray

How to damage gameobject via raycast

Hi i'm looking the best way to apply damage via raycast.

using this script, I know the tag gameobject hit, but how can I apply damage?

I have many gameobject with the same tags.

i need other script attached gameobject enemy?

SendMessage can be the solution? how to use this?

     var shoot: GUITexture;
     public var camera1:Camera;
     var crosshairs:Texture;
     
 
     
     function Awake(){
     shoot= GameObject.Find("shoot").guiTexture;
     
     }
     
     function Update () {
     
     for(var touch : Touch in Input.touches){
     if(touch.phase == TouchPhase.Ended && shoot.HitTest (touch.position))){
     animation.Play("shoot");
     audio.Play();
     ShootFunction();
    }
     }
 
 function OnGUI() {
      GUI.DrawTexture(new Rect(Screen.width / 2, Screen.height / 2, 50, 50), crosshairs);
 }
 
 function ShootFunction()
     {
         var ray : Ray = camera1.ViewportPointToRay (Vector3(0.5,0.5,0));
         var hit:RaycastHit;
         if (Physics.Raycast(ray, hit)){
          Debug.Log("Hit " + hit.collider.tag);}
         
     }
 
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 AlucardJay · Apr 04, 2013 at 04:54 PM 1
Share

Don't forget Send$$anonymous$$essage :

 hit.collider.gameObject.Send$$anonymous$$essage( "DealDamage", theAmount, Send$$anonymous$$essageOptions.DontRequireReceiver );

  • http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Send$$anonymous$$essage.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Send$$anonymous$$essageOptions.html

1 Reply

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

Answer by jmatthews · Apr 03, 2013 at 11:39 PM

 hit.collider.gameObject.GetComponent<ComponentName>().CallWhateverFunctionHandlesDamage();
Comment
Add comment · Show 7 · 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 crowley91t · Apr 04, 2013 at 12:57 AM 0
Share

your is my hit.collider.tag?
can you explain better your code?

avatar image jmatthews · Apr 04, 2013 at 01:01 AM 0
Share

Sure, you already have the hit.collider, you just need to use that to reference the instance of that gameObject, via

 hit.collider.gameObject.GetComponent<ComponentName>().CallaFunctionHere
avatar image crowley91t · Apr 04, 2013 at 01:24 AM 0
Share

and if I want to manage the health of the enemy, with another script?

thank you very much for your help =)

avatar image jmatthews · Apr 04, 2013 at 02:36 PM 0
Share

you're welcome.

avatar image jmatthews · Apr 04, 2013 at 04:55 PM 1
Share

you'd be calling

 hit.collider.gameObject.GetComponent<HealthScriptName>().NameoftheFunctionthatHandlesHealth
Show more comments

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

12 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

Related Questions

Bug/Error in script CollectPapers 0 Answers

Detect Object that are hit by a ray 3 Answers

Shoot off multiple raycasts from 1 object? 2 Answers

Need help with building system 1 Answer

Creating a teleportation gun 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