Instantiated object to move along the rays direction where there is no hit! JS Only

Hi,
Can you please help me with this, I couldn’t find answer for it :(. I’m trying to create a FPS for iOS, in which I have a crosshair that moves with joystick. This is the “crosshair Script” which is attached to the camera:

var ray : Ray = camera.ViewportPointToRay (Vector3(crosshairGUI.transform.position.x  , crosshairGUI.transform.position.y, 0));
//As I said the joystick changes the position of the crosshairGUI. 

//this part detects if the ray hits any object in the scene
var hit : RaycastHit;

//check if the ray hits any object, if so send that location to the bullet prefab's script
if (isGunTriggered)
{
         if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))
		    {
		    	var bullet=Instantiate(bullets, transform.position, transform.rotation);
		    	bullet.BroadcastMessage ("hitLoc", hit.point);
		    	bullet.BroadcastMessage ("hitStatus", true);
		    	gunIsTriggered = false;
		    }
}

Now on the my bullet prefab, I have added rigidbody and collider. ray.direction and hit.point info are sent from crosshair script. In this script I used hit.point, so that the bullet would look at that point:

//these are codes inside the fixedUpdate function of the bullet:
        if (hitStatus)     //also has been received from crosshair script
        {
       //assigned the received the hit.point and assign it to the hitLocation in Start()
		transform.LookAt(hitLocation);
		rigidbody.AddRelativeForce(0, 0 , 1 * forwardForce);   //forward force is 1000
        }
        else
        {
             ...... // explain it in the next section!
        }

OK, up until here, as long as there are objects that rays hit it, the games runs just fine, bullets shoot at those points. BUT where ever there is no object (like Sky), the hit.point is no longer useful, so I have that else statement so in that case, bullet should follow the ray.direction AND THAT IS MY ISSUE. I wrote :

else
{
     rigidbody.AddForce(rayDirection * forwardForce);
}

The problem is that, the bullets will be shooted where the crosshair is, but the orientation of the bullets (which are scaled cubes “or rectangles”) are not correct. does anyone know how to solve this? how shoot along the ray’s direction where there is no hit?

To set the orientation try adding:

transform.rotation = Quaternion.LookRotation(rayDirection);

Thanks a lot bro :wink: problem solved!