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 SendMessage : hit.collider.gameObject.SendMessage( "DealDamage", theAmount, SendMessageOptions.DontRequireReceiver ); * http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html * http://docs.unity3d.com/Documentation/ScriptReference/SendMessageOptions.html

–

1 Answer

1

hit.collider.gameObject.GetComponent().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're welcome.

–

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

–