Help With Laser Effect - Perlin


So I'm working on this laser turret and I'm using the Perlin effect with the Lightning Bolt script (mines called LaserBolt), the laser beam starts at the barrel of the turret, and finishes on a target (an enemy). The turret rotates to face any enemy that is in range and fires the bolt. The problem is, is that the bolt bends.

Basically, I want the bolt to fire in a straight direction from the turret to the enemy (target) - instead of the bolt fixed on a target and changing its angle ...

Its hard to explain, so here is a video to show you:

I also have no idea about C#

using UnityEngine;
using System.Collections;

public class LaserBolt : MonoBehaviour
    public Transform target;
    int zigs = 100;
    float speed = 0.8f;
    float scale = 0.4f;
    public Light startLight;
    public Light endLight;

    public bool activeLaser = false;

    Perlin noise;
    float oneOverZigs;

    private Particle[] particles;

    void Start(){
        oneOverZigs = 1f / (float)zigs;
        particleEmitter.emit = false;

        particles = particleEmitter.particles;

    void Update (){
        if(target == null){
            target = null;

    if(activeLaser == false){
        particleEmitter.enabled = false;
        particleEmitter.emit = false;
        renderer.enabled = false;
        target = null;

    else if(activeLaser == true){
        particleEmitter.enabled = true;
        renderer.enabled = true;
        if (noise == null)
            noise = new Perlin();

        float timex = Time.time * speed * 0.1365143f;
        float timey = Time.time * speed * 1.21688f;
        float timez = Time.time * speed * 2.5564f;

        for (int i=0; i < particles.Length; i++)
            Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);
            Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z),
                                        noise.Noise(timey + position.x, timey + position.y, timey + position.z), //),
                                        noise.Noise(timez + position.x, timez + position.y, timez + position.z));
            position += (offset * scale * ((float)i * oneOverZigs));

            particles*.position = position;*
 _particles*.color = Color.white;*_
 <em>_particles*.energy = 1f;*_</em>
 <em>_*particleEmitter.particles = particles;*_</em>
 <em>_*if (particleEmitter.particleCount >= 2)*_</em>
 <em>_*if (startLight)*_</em>
 <em>_*startLight.transform.position = particles[0].position;*_</em>
 <em>_*if (endLight)*_</em>
 <em>_*endLight.transform.position = particles[particles.Length - 1].position;*_</em>
<em>_*<p>So, I really just want it to fire straight on to a target, instead of moving the finish transform.position.</p>*_</em>
<em>_*<p>// ---- edit ---- \</p>*_</em>
<em>_*<p>This line here needs adjusting I think:</p>*_</em>
<em><em>_Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);_</em></em>
<em>_*<p>I just need to change it so that it ignores the target.position.x ... how would I do that in C# ?</p>*_</em>
<em>_*<p>Would appreciate it, thanks</p>*_</em>
<em>_*<p>// ---- Edit ----- \</p>*_</em>
<em>_*<p>So I added this:</p>*_</em>
<em>_*Vector3 NoXComponent = target.position;*_</em>
<em>_*NoXComponent.x = 0;*_</em>
<em><em>_Vector3 position = Vector3.Lerp(transform.position, NoXComponent, oneOverZigs * (float)i);_</em></em>
<em>_*<p>And now I get this:</p>*_</em>
<em>_*<p><img src="" alt="alt text"></p>*_</em>
<em>_*<p>I'm guessing its because thats where the 0 coordinate is for X</p>*_</em>

Looking at the video, I would say your Lerp stuff works fine. The green path goes exactly where you want it to. What seems to be the problem is the angle the gun is facing. You seem to have a really smooth (read slow) interpolate on that, so it is lagging behind its target too far. That's what makes it look like the beam is not coming out right. Try taking the interpolate off the rotation, and see if it looks any better.

Let me see if I understand. You want the beam to leave the gun directly from its barrel and go straight ahead whenever the target is close to in front of the gun? As you have the Lerp now, the gun will not track the target the entire time, so the question is if you want to Lerp faster or fire when not aimed directly at the target, only nearby.

For the latter, create a ray using the transform.forward of your gun. Then raycast down that path (if you don't want it to fire unless a direct hit). Otherwise, set your target of the beam to the end of the ray (i.e. rayVector [which is transform.forward] * distance) and use that. The beam will always be coming out forward and will "sweep" across the target...