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 Zold · Mar 18, 2013 at 08:12 PM · raycastmouseline renderer

How to make a line renderer (laser beam) smoothly follow the mouse?

SOLVED

To make a line renderer or laser smoothly follow the mouse, and yet still stop at colliders without snapping to them I used to following workaround:

Using the Angrybots 'LaserScope' and 'Per Frame Raycast' scripts, I adjusted to following:

Per Frame Raycast: (Change the script into the following)

 private var hitInfo : RaycastHit;
 private var tr : Transform;
 var LaserPoint : Transform;
 var rotationSpeed = 3; 
           
 function Awake () {
        tr = transform;
 }
 
 function Update () {
     // Cast a ray to find out the end point of the laser
        transform.LookAt(LaserPoint);
           Physics.Raycast (tr.position, tr.forward, hitInfo);
 }

 
 function GetHitInfo () : RaycastHit {
         return hitInfo;
 }

Next, make an empty game object that will follow your mouse.

Attach the following script onto the Empty Game Object:

 var DistancefromCamera = 0.00; //Preferably high amounts)
     
 function Update(){
 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var pos : Vector3 = ray.GetPoint(DistancefromCamera);
         transform.position = pos;
 }

Now, you can just attach the 'LaserScope' and 'Per Frame Raycast' script on your object which should fire a laserbeam. (Note that you might want to use a Laserdot object, or just copy the one from Angrybots)

Assign the 'Mouse Following Empty Game Object' onto the 'Laser Point' in the inspector... and that's about it! (Laser Point can be found under Per Frame Raycast).

I am sure this isn't the best workaround, and the scripts might be formatted poorly, or contain flaws (as I am quite new to scripting) but, it works great for me!

Comment
Add comment · Show 1
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 Owen-Reynolds · Mar 20, 2013 at 01:38 PM 0
Share

Biggest possible improvement is Raycasting doesn't need any gameObjects. There are several(?) threads here on that. For example, Quaternion.LookDirection can be used ins$$anonymous$$d of LookAt to figure which way gameObject A would be looking at B, if they existed (which they don't.) Sometimes "just math" ray casts can be easier to write.

If you had 100 items all making an extra gameObject and lagging, might be worth changing. But for just one, don't change what works.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Mar 18, 2013 at 11:14 PM

The code you're looking at seems to use hit.normal to pull back a little, maybe to avoid overlapping? The problem is, the normal could just as easily move it sideways. The ray end point would have some odd jerks as you moved it over the surface -- almost like a magnet was pushing it. Could remove hit.normal and see how it looks.

For a real pullback, something like: Vector3 unitRay = (rayEnd-rayStart).normalized; rayEnd -= unitRay*0.4f;. That would pull the end back 0.4 meters, without changing the aim point.

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 Zold · Mar 19, 2013 at 09:47 PM 0
Share

Thanks for the answer, I tried removing the normal, and it did certainly make it better, but not perfectly smooth.

Your answer got me thinking however, and I found a workaround using the Angrybots laser and adjusting that script a little here and there.

I will post my workaround, and regard this question answered. Thanks for your help!

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turn in the direction of movement 0 Answers

Mouse plane does not detect height 1 Answer

Line Renderer Rotation is off. 0 Answers

OnMouseExit Problems vs. RaycastAll 2 Answers

Detecting if the object is hit by LineRenderer 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