raycasting and linerenderer and direction

Howdy,
I’ve searched around and I apologize if I’ve overlooked anything to explain this to me, but I didn’t find much as of yet.

So, in my little test game I have a spaceship model that flies around and all the controls work great thus far. Forward, back, side to side, and all maintain velocity as I want. So far so good.

Now I have a pair of linerenderers setup for a laser effect, and attached raycast colliders to them. I’m not actually sure, now that I think of it, if that collider should go in the beam or the objects to be hit, but that’s easy to test and neither here nor there. No, what I’m having trouble is with directing my raycast, I think.

I have this code here-

for(var shoot : LineRenderer in gun)
      {
        var ray = new Ray (transform.position, transform.TransformDirection(Vector3.forward));
        if (Physics.Raycast (ray, hit, firePoint)) {
        shoot.SetPosition(0, hit.point);
       }else {
         shoot.SetPosition(0, ray.GetPoint(firePoint));
       }
     }

The problem I’m running into is that this causes the lines to draw off towards the corners of the screen as I move the mouse around (mouselook is attached to the ship model) instead of straight ahead.

What am I misunderstanding here?

Thanks![/code]

Have you got this code on your camera script? I would expect it to work if it is on the spaceship. If not, can you post the code for the SetPosition function?

I have two empty gameobjects, hierarchically within the ship object, that I’m using to create the laser effect and that’s where I have the script attached. What’s interesting is that, with my current code, it seems the ray is actually doing what I’d expect, because I have it currently set so the line is only visible when the ray hits something, but the linerenderer is jutting off towards the upper left.

Here’s the entirety of the script:

var gun;
//var firePoint = Vector3(0, 0, 1000);
var firePoint = 1000;
var gleft = true;
function Awake()
{
   gun  = GetComponentsInChildren(LineRenderer);
}
function Update () 
{
  var fwd = transform.TransformDirection (Vector3.forward);
  if(Time.timeScale != 0)
  {
  if(Input.GetKey("space"))
  {
    var hit : RaycastHit; 
   
	for(var shoot : LineRenderer in gun)
	{
		var ray = new Ray (transform.position, transform.TransformDirection(Vector3.forward));
	    if (Physics.Raycast (ray, hit, firePoint)) {
         shoot.SetPosition(0, hit.point);
       }else {
         //shoot.SetPosition(0, ray.GetPoint(firePoint));
       } 
	}
  }else{
  for(var shoot : LineRenderer in gun)
	{
	  shoot.SetPosition(0, Vector3(0, 0, 0));
	}
  }
  }
}

I altered

shoot.SetPosition(0, hit.point));

to

shoot.SetPosition(0, Vector3(0, 0, Mathf.Abs(hit.point.z)));

and that seems to work fine. I don’t rightly know why though. I mean, I get the general idea but I don’t know why the x and y coordinates returned by hit aren’t what I expected, so any insights would be greatly appreciated.

In case it helps anyone else, I just saw that raycasthit contains a distance property, allowing me to eliminate the absolute value call.