how to specify direction in raycast ....??

i am trying to make gun shoot i want to use raycast … i made bullet shoot without using physics using deltatime… now i want to use raycast to destroy the target when it detect a collider

To fire a raycast, it’s pretty simple. All it needs to know, is where to fire the cast from, and where to:

var direction = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
var damage : float = 10;


if(Physics.Raycast (transform.position, direction, hit, Mathf.Infinity)){
	Debug.DrawLine (transform.position, hit.point, Color.red, 1);	
	
	//Make sure you have a function called ApplyDamage on the object it wants to give damage to
	hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);

}

This will fire the ray from the transform (player) the script is attached to, and it will fire it in the direction the player is facing (Z axis).

Also, the ‘hit.collider.SendMessage’ is to trigger a function called ‘ApplyDamage’ on the object the raycast fires… thats if there is a function to call. So in other words, on the enemy health script, you want a function like this:

function ApplyDamage(damage : float){

	health -= damage;
}