- Home /
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);}
}
Don't forget Send$$anonymous$$essage :
hit.collider.gameObject.Send$$anonymous$$essage( "DealDamage", theAmount, Send$$anonymous$$essageOptions.DontRequireReceiver );
Answer by jmatthews · Apr 03, 2013 at 11:39 PM
hit.collider.gameObject.GetComponent<ComponentName>().CallWhateverFunctionHandlesDamage();
your is my hit.collider.tag?
can you explain better your code?
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
and if I want to manage the health of the enemy, with another script?
thank you very much for your help =)
you'd be calling
hit.collider.gameObject.GetComponent<HealthScriptName>().NameoftheFunctionthatHandlesHealth
Your answer
Follow this Question
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