Hi there,
I have a script that i use to Raycast to an object then send a message to it to execute a function. It works, the problem being that it is very unreliable and seems to act on a kind of delay.
For example, i will approach my enemy, looking directly at him and it is only when I go around to the back of his model and stare at it that the Raycast hits him, sends the message and then executes the desired function. Here is both the Raycast script and the script attached to the enemy.
var hit : RaycastHit;
var TheDammage : int = 100;
function Update()
{
if ( Physics.Raycast( transform.position, transform.forward, hit, 3 ))
{
Debug.Log( "ray hit (tag) : " + hit.collider.gameObject.tag + " : " );
if ( hit.collider.gameObject.tag == "EnemyStatic" )
{
Debug.Log("HitStaticEnemy");
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
#pragma strict
@script RequireComponent(AudioSource)
var Soundplayed1 = false;
var HorrorHit1: AudioSource;
var Health = 100;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
if(Soundplayed1 == false)
{
Dead();
}
}
function Dead()
{
GetComponentInChildren(MeshRenderer).enabled = false;
Debug.Log("MeshHit");
audio.volume = 1.0;
HorrorHit1.Play();
Soundplayed1 = true;
Destroy (gameObject, 5);
}