Character lasergun?

Hi, I've been looking for a scrip that creates a laser from the characters gun. The game is in Top-down view and so i don't want a cursor or anything i just want a laser to go from the specific character point(Maybe an object that beams out the laser forward of the character?). However, I managed to make a laser with the class LineRenderer, but... I wasn't able to get it to follow the character while he was turning or even get it too look right since it's "glowing". I want a red and transparent straight line to go forward.

So to define exactly what i'm looking for i'll list it nicely:

  • A "laserbeam" (Red and transparent)

  • Coming from an object which is connected to the Player prefab (So it will follow while the character spinns)

  • stops when hitting a tree or another object

(- and can be turned off by a button click)

Nor should it continue forward for all eternity either, the line length should be editable. Could this be done by using some kind if sprite? How?

I know its much to ask for but i've been looking everywhere for this!:shock: Would be nice if this could be shared with the Unity3D community since all i've found is people who are looking for the exact same thing.

Ps: Scripting isn't my strong side so be nice & explain carefully!

Thanks, N0tiC!

alt text

I have a script where I'm doing something like this, but with several lines. Ingore the 'first line' part of the script, just read 'laser'. I should've edited but.. I'm lazy. ;)

var maxDistance : float = 2

function Start () {
    GetComponent(LineRenderer).useWorldSpace = true;
}

function Update () {
    var laser = GetComponent(LineRenderer);
    var forward = transform.forward;
    var hit : RaycastHit;

    firstLine.SetPosition(0, transform.position);

    if(Physics.Raycast(transform.position, forward, hit) && Vector3.Distance(transform,position, hit) >= maxDistance)){
        laser.SetPosition(1, hit.point);

    }
    else{
        laser.SetPosition(1, transform.TransformDirection(0, 0, MaxDistance));
    }
}

Attach this to an empty gameobject which is childed to your gun. Make sure the z-axis is pointing 'character forward'.