Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CrazyLule · Jan 10, 2017 at 11:33 PM · raycasthitray

How do I detect if object is geting hit by ray

So , I have a ray thats created evry time i click mouse button , and ray can detect what its hitting , but how do I detect of other objects side if its geting hit by the ray so I can take some health away

using UnityEngine; using System.Collections;

public class shoot : MonoBehaviour {

 Ray ray;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     RaycastHit hit;



     if (Input.GetMouseButtonDown (0)) {
         if (Physics.Raycast (transform.position, transform.forward, out hit)) {
             Debug.Log (hit.transform.name);
             Debug.DrawRay (transform.position, transform.forward, Color.red, 25f);
         
         }
     }

 }

}

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

Answer by milleniu · Jan 10, 2017 at 11:53 PM

Since you have the object hit saved in the Raycast hit variable, you can use the GetComponent function : Documentation for Get Component and then call a public function on the GameObject.

For example, imagine the script you have on the object you want to hit is named ActorEnemyManager which have a public function Damage(float amount), then you could do the following :

          if (Physics.Raycast (transform.position, transform.forward, out hit)) {
              Debug.Log (hit.transform.name);
              Debug.DrawRay (transform.position, transform.forward, Color.red, 25f);
          
              ActorEnemyManager actorEnemyManager = hit.transform.GetComponent<ActorEnemyManager>();
              if (actorEnemyManager != null) //  Checking if we found a component
                 actorEnemyManager.Damage(10.0f);
          }

Comment
Add comment · Show 2 · 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 CrazyLule · Jan 11, 2017 at 12:05 PM 0
Share

When I do this error is

Cannot convert method group GetComponent' to non-delegate type actorenemymanager'. Consider using parentheses to invoke the method

using UnityEngine; using System.Collections;

public class ray : $$anonymous$$onoBehaviour {

 RaycastHit hit;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.Get$$anonymous$$ouseButtonDown (1)) {
         Debug.DrawRay (transform.position, transform.forward, Color.green, 3);
         if(Physics.Raycast(transform.position,transform.forward,out hit,300))
             Debug.Log(hit.transform.name);
         actorenemymanager actorenemy = hit.transform.GetComponent<actorenemymanager>;


     }

     }
 


}

avatar image milleniu CrazyLule · Jan 11, 2017 at 01:12 PM 0
Share

You forgot () on the line with GetComponent :

  actorenemymanager actorenemy = hit.transform.GetComponent<actorenemymanager>;

It should be :

  actorenemymanager actorenemy = hit.transform.GetComponent<actorenemymanager>();

It looked weird on my initial answer because they got moved to a new line for some reason.

avatar image
0

Answer by JoshBaz · Jan 11, 2017 at 01:04 AM

You could use use two classes. One sending the ray and one receiving the ray. When the ray hits the object use GetComponent to get a class attached to the recieving object and then call a public method to apply damage. Also in your question you said click. If you want to call the function on a click you should use Input.GetMouseButton and use Input.GetMouseButtonDown if you want to call it the method whenever the mouse button is being held down. In the class sending the ray:

 public float damage = 100f;
 
 void Update(){
     if(Input.GetMouseButton(0))
         RayClick();
 }
 
 void RayClick (){
     RaycastHit hit = new RaycastHit;
 
     if (Physics.Raycast (transform.position, transform.forward, out hit)){
         ReceivingClass rc = hit.transform.GetComponent<ReceivingClass>();
     if(rc != null)
         rc.Damage(damage);
     }
 }

Then in the class to receive the damage:

 public float health = 500f;
 
 public void Damage(float damageReceived){
     health -= damageReceived;
 
     if(health <= 0f)
         //Die...
 }


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

62 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

Related Questions

Raycast won't hit Collider 1 Answer

set ray.point as x, y and z float 1 Answer

How to get information from raycast? 2 Answers

set ray only when raycast a specific layer 1 Answer

How to get a Vector3 from a RaycastHit variable? 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