So I want a script on an object which checks if itself has been hit by a ray.
How would I do this?
Here’s my script for the ray. (Ignore empty if statement)
#pragma strict
var enemy : Transform;
function Update () {
if (Input.GetMouseButtonUp(0)) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray)) {
}
}
}
}
Thanks in advance.
BTW I would like all answers in unityscript I don’t like using c#
If I understand well, you have tried Destroy(“enemy”) inside the if() statement between lines 11 - 14 of your code, but the wrong enemy dies, i.e. not the one that gets hit by the ray. If this is the case, the problem is that your are not referencing the object hit properly.
You need to define a RayCastHit object, in order to get a reference to the collider of the object that was actually hit by the ray. See this example:
… but instead of the example’s hit.collider.enabled = false;
you need to use Destroy(hit.collider.gameObject);