How can I can I cast a ray from a gameobject?

So I’m trying to cast a simple ray from the spawn object, because when I tried to instantiate a bullet and fire it, it didn’t work. So I decided to use a raycast instead, but it doesn’t seem to work. In this scrpt I have it so that a ray is fired, and a linerenderer shows the position of where its fired, but the linerenderer doesn’t move when clicked. Any help would be greatly appreciated, here is my script:

#pragma strict

var c1 : Color = Color.yellow;
var c2 : Color =;
var objecthit : GUIText;
var lineRenderer : LineRenderer;
function Start() { 
     lineRenderer = gameObject.AddComponent(LineRenderer);
     lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
     lineRenderer.SetColors(c1, c2);

 function Update () {
        var fwd = transform.TransformDirection (Vector3.forward);
        var origin = transform.position;
var direction = transform.forward;
var endPoint = origin + direction * 100000;
var hit : RaycastHit;
lineRenderer.SetPosition(0, origin);
 if(Input.GetMouseButtonDown(0) || (Input.GetMouseButtonDown(1)) || (Input.GetMouseButtonDown(2))){
        if (Physics.Raycast (transform.position, fwd, 10)) {
            print ("There is something in front of the object!");
            objecthit.text = "You Hit: " +;

I thought I knew Unity well, an I’m not a beginner, but I guess I just forgot quite a bit of the basics.

Oh and I don’t mean camera screen point to ray I have to have the object fired from the spawn point (the tip of the pistol) because of how I control the game.

Thanks again,


In a quick read, you have two problems. You don’t pass ‘hit’ in to the Raycast(), so it is not initialized. So your use of ‘hit’ inside the ‘if’ statement is uninitialized. I’m surprised you did not get a runtime error. The second problem is that you never set the second point of the line renderer, so my guess is that the other end will be (0,0,0). Use a form of the Raycast() that takes a RaycastHit as a parameter, and use hit.point at the second point in the LineRenderer. As a side note, take a look in the reference for ‘Transform.forward.’ You don’t need the TransformDirection().