Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by captainkrisu · Oct 16, 2015 at 06:08 PM · unity 5raycastlinerendererbeam

[C#] Line Renderer beam not going straight

Hello!

I need help.

I'm making a Wave Motion Gun from Space Battleship Yamato. The script shoots a straight beam if nothing is in front of it, and if there is, end position of the line renderer would be the RaycastHit point.

Picture of it working firing straight into space: PICTURE

Picture of hitting a target: PICTURE2

Picture after target was destroyed, the beam isn't shooting straight anymore: PICTURE3

This is the code:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(LineRenderer))]
 
 public class WaveCannon : MonoBehaviour
 {
 
     public float Damage = 1.0f;
 
     public AudioSource audioSource;
 
     public Transform LaserFiringPoint;
     public float LaserMaxDistance = 10.0f;
 
     LineRenderer linerenderer;
 
     public AudioClip WaveGunCharge;
     public AudioClip WaveGunPrefire;
     public AudioClip WaveGunLaser;
 
     public ParticleSystem ChargingObject;
     public ParticleSystem PrefireObject;
     public ParticleSystem FiringObject;
     public GameObject FiringEndObject;
 
     public float LaserReachSpeed;
 
     bool Firing = false;
 
     private float beginTime;
 
     Vector3 HitPoint;
 
     private bool canDamage = false;
 
     GameObject hitThing;
 
 
     void Start()
     {
         linerenderer = GetComponent<LineRenderer>();
         linerenderer.GetComponent<Renderer>().enabled = false;
         Firing = false;
 
         ChargingObject.Stop();
         PrefireObject.Stop();
         FiringObject.Stop();
     }
 
     void Update()
     {
 
         if (Firing)
         {
 
             RaycastHit hit;
 
             float smoothMove = (Time.time - beginTime) * LaserReachSpeed;
             linerenderer.SetPosition(0, LaserFiringPoint.position);
 
             Vector3 fwd = LaserFiringPoint.TransformDirection(Vector3.forward);
 
             if (Physics.Raycast(LaserFiringPoint.position, fwd, out hit, LaserMaxDistance))
             {
                 linerenderer.SetPosition(1, Vector3.Lerp(LaserFiringPoint.position, hit.point, smoothMove));
                 Debug.DrawLine(LaserFiringPoint.position, hit.point);
                 HitPoint = hit.point;
                 canDamage = true;
                 hitThing = hit.transform.gameObject;
             }
             else
             {
                 linerenderer.SetPosition(1, Vector3.Lerp(LaserFiringPoint.position, fwd * LaserMaxDistance, smoothMove));
                 canDamage = false;
                 hitThing = null;
             }
 
         }
         else
         {
 
             if (Input.GetKeyDown("g"))
             {
                 Charging();
             }
 
         }
 
     }
 
     void Charging()
     {
 
         ChargingObject.Play();
         audioSource.PlayOneShot(WaveGunCharge);
         Invoke("Prefire", WaveGunCharge.length);
 
     }
 
 
     void Prefire()
     {
 
         PrefireObject.Play();
         ChargingObject.Stop();
         audioSource.PlayOneShot(WaveGunPrefire);
         Invoke("Fire", WaveGunPrefire.length);
 
     }
 
     void Fire()
     {
         beginTime = Time.time;
         Firing = true;
         FiringObject.Play();
         PrefireObject.Stop();
         linerenderer.GetComponent<Renderer>().enabled = true;
         audioSource.PlayOneShot(WaveGunLaser);
         Invoke("AfterFire", WaveGunLaser.length);
         InvokeRepeating("DamageEffect", 0.0f, 0.05f);
 
     }
 
     void DamageEffect()
     {
         if (canDamage)
         {
             Instantiate(FiringEndObject, HitPoint, transform.rotation);
             hitThing.SendMessage("Damage", Damage, SendMessageOptions.DontRequireReceiver);
         }
     }
 
 
     void AfterFire()
     {
         CancelInvoke("DamageEffect");
         FiringObject.Stop();
         linerenderer.GetComponent<Renderer>().enabled = false;
         Firing = false;
     }
 
 
 }


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 Oribow · Oct 16, 2015 at 07:59 PM 0
Share

Are you moving the ship or is it stationary?

avatar image captainkrisu Oribow · Oct 16, 2015 at 09:04 PM 0
Share

While I took the pictures, I did not move it. But it does the it while moving too.

1 Reply

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

Answer by Bunny83 · Oct 16, 2015 at 09:57 PM

Well your problem is that in the case where you don't hit anything you set the second point to:

 Vector3.Lerp(LaserFiringPoint.position, fwd * LaserMaxDistance, smoothMove)

The problem here is that "LaserFiringPoint.position" is a world space point but "fwd * LaserMaxDistance" is just a world space direction and not a point. So the resulting positiob will be around the world origin.

You might want to use this instead:

 linerenderer.SetPosition(1, LaserFiringPoint.position + fwd * LaserMaxDistance * Mathf.Clamp01(smoothMove));
Comment
Add comment · Show 1 · 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 captainkrisu · Oct 17, 2015 at 10:01 AM 0
Share

That did it! Thanks for helping with the problem! That will actually help with couple other projects too, that had the problem. Thank you.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast.distance giving me the wrong number 2 Answers

Raycasts in Multiplayer Network 0 Answers

Raycast not updating inside a trigger collider 0 Answers

How, from scratch, can I detect the angle of the ground underfoot a character not controlled by a character controller. 1 Answer

Printing Distance b/w two points using line renderer and Raycast 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