Destory Ray On Object Collision

How do i destroy a ray up to the point where it collides with an object. For example, How do i make it so that the ray will only go up to the wall and not through it, si the ray will no recognize anything thats behind a wall?

Here is my Raycast Shooting Script for an FPS sp far…

function Update () {


	if(Input.GetButton("Fire1")){
		FireWeapon(15.0, 215.0, 10.0);
	}


}

function FireWeapon (Range : float, Force : float, TimeBetweenShots : float){
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);

Debug.DrawRay(transform.position , DirectionRay * Range , Color.blue);

	if(Physics.Raycast(transform.position , DirectionRay, Hit , Range)){

		if(Hit.rigidbody){
			Hit.rigidbody.AddForceAtPosition ( DirectionRay * Force, Hit.point);
		}
	}
	

	
}

Any help is appreciated

The functionality of the Physics.Raycast is already what you ask for. As long as it is a collider on the wall it will only return what’s in front of the wall or the wall itself.

What probably confuses you is that you draw the ray at a given range.

Try doing this instead after you’ve declared ‘Hit’:

Debug.DrawLine(transform.position, Hit.point, Color.blue);

make a layer called ignoreCollision. add a collider and rigidbody to your wall and set the wall’s layer to ignoreCollision. after your if (Hit.rigidbody) throw in a if (Hit.gameObject.layer!=“ignoreCollision”). now the wall can eat up the raycast