Raycast from an objects co-ordinates

I am trying to make a ray cast from an empty object at the end of a weapon barrel. The problem is the only raycasts I see being used are using ScreenPointToRay, is there a way to fire a ray from the co-ordinates of an object instead of a point on the screen?

A ray can be cast from anywhere, ScreenPointToRay is just a method of converting screen coordinates to world coordinates.

var hit : RaycastHit;
if ( Physics.Raycast( transform.position, transform.forward, hit, 100.0 ) )
    Debug.Log( " Ray Hit " + hit.collider.gameObject.name );

this will cast a ray from the object, in the direction of the object forward (+Z-axis), for a distance of 100.0 units, then print in the console the name of anything that was hit =]

Here is a video for raycasting : Unity 3D Student - 3d Modelling

Edit :

Normally if you are raycasting to shoot a bullet, there is no bullet, the raycast is used to find : if it hit enemy then deduct health; if the ray hit a wall then put a bullethole image at the ray hit point (and play an audio clip at hit point).

If you are using real bullets and physics, there is no need for a raycast at all. The bullet has a rigidbody with gravity enabled, simply fire the bullet (instantiate it), set the velocity or add impulse force at the Start, then let the physics take over.

  • I’m using it in a coroutine - Check below for a bullet timer and firing script.

  • I want to simulate realistic bullet drop - You should ask this as a separate question, but some of the results from this search should help : unity fire arrow - Google Search

If my answer solves the original question Raycast from an objects co-ordinates, could you please mark it as answered, thanks.

here is a script just for firing bullets with a delay between each bullet :

public var bulletObject : Transform;
public var bulletSocket : Transform;

private var bulletTimer : float = 0.0;
public var bulletTimerMax : float = 0.06;

function Update() 

function ShootBullet() 
	bulletTimer -= Time.deltaTime;
	if ( Input.GetMouseButtonDown(0) )
		if ( bulletTimer < 0.0 )
			Instantiate( bulletObject, bulletSocket.position, bulletSocket.rotation );
			bulletTimer = bulletTimerMax;