Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oliver-jones · May 11, 2011 at 11:57 AM · angletargetperlin

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:

alt text

I'm guessing its because thats where the 0 coordinate is for X

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · May 11, 2011 at 12:22 PM 0
Share

So I just want to ignore the X coordinates on the target ... how would I do that?

avatar image almo · May 11, 2011 at 01:46 PM 0
Share

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).

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · May 11, 2011 at 12:44 PM 0
Share

Odd -- I thought this would work - but its not, it fires the beam in completely the wrong direction. Any suggestions why?

avatar image oliver-jones · May 11, 2011 at 12:47 PM 0
Share

Look at my updated answer -- the photo

avatar image almo · May 11, 2011 at 01:07 PM 0
Share

Can you show a pic of what happens without ignoring the X component?

avatar image oliver-jones · May 11, 2011 at 01:18 PM 0
Share

Its on the video in my post - but basically the beam doesn't fire straight from the turret.

avatar image almo · May 11, 2011 at 01:28 PM 0
Share

It says the video is private, so it won't let me see it.

Show more comments
avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges