- Home /
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);
}
}
}
}
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);
}
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>;
}
}
}
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.
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...
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612104548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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