- Home /
Help With Laser Effect - Perlin
Okay,
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:
http://www.youtube.com/watch?v=VxB21J-RG5o
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;
particleEmitter.Emit(zigs);
particles = particleEmitter.particles;
}
void Update (){
if(target == null){
target = null;
}
if(activeLaser == false){
particleEmitter.enabled = false;
particleEmitter.emit = false;
renderer.enabled = false;
target = null;
endLight.gameObject.SetActiveRecursively(false);
}
else if(activeLaser == true){
particleEmitter.enabled = true;
renderer.enabled = true;
endLight.gameObject.SetActiveRecursively(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[i].position = position;
particles[i].color = Color.white;
particles[i].energy = 1f;
}
particleEmitter.particles = particles;
if (particleEmitter.particleCount >= 2)
{
if (startLight)
startLight.transform.position = particles[0].position;
if (endLight)
endLight.transform.position = particles[particles.Length - 1].position;
}
}
} }
So, I really just want it to fire straight on to a target, instead of moving the finish transform.position.
// ---- edit ---- \
This line here needs adjusting I think:
Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);
I just need to change it so that it ignores the target.position.x ... how would I do that in C# ?
Would appreciate it, thanks
// ---- Edit ----- \
So I added this:
Vector3 NoXComponent = target.position;
NoXComponent.x = 0;
Vector3 position = Vector3.Lerp(transform.position, NoXComponent, oneOverZigs * (float)i);
And now I get this:
I'm guessing its because thats where the 0 coordinate is for X
So I just want to ignore the X coordinates on the target ... how would I do that?
One $$anonymous$$or detail that I don't think is causing the problem... you loop over the number of particles, but interpolate with (oneOverZigs * i). Ordinarily Zigs = particles, but I would be paranoid and just loop over the Zigs, or interpolate to (oneOverParticles).
Answer by almo · May 11, 2011 at 12:37 PM
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.
Odd -- I thought this would work - but its not, it fires the beam in completely the wrong direction. Any suggestions why?
Can you show a pic of what happens without ignoring the X component?
Its on the video in my post - but basically the beam doesn't fire straight from the turret.
Answer by The_r0nin · May 11, 2011 at 03:04 PM
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...
Your answer
Follow this Question
Related Questions
camera target keep angle and y position 1 Answer
Else Statement Not Working 1 Answer
Angle to Direction 1 Answer
Find angle to target (not angle to rotate by) 2 Answers
Vector3.Angle() Never Reaching 0 1 Answer