• 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 Jason B · Aug 11, 2011 at 09:51 PM · c#raycastcolliderlaser

Can't get a laser working properly.

I'm using a LineRenderer to trace a laser. The second point of the LineRenderer has to be wherever it's coming in contact with a collider, and if it's not contacting one, it should maintain a set length.

T$$anonymous$$s isn't working at all. It's just INCREDIBLY unstable and makes these weird, random movements (the end point sways around at random).

Not sure what I'm doing wrong here, as the code is as basic as it gets.

It pretty much works the way I need it to, except that as my character moves left and right, the end point of the laser sways GREATLY left and right, resulting in a laser that won't stay straight unless I'm in the middle of the screen.

 void SetLength()
     {
         RaycastHit LaserHit = new RaycastHit();
         if (Physics.Raycast(new Ray(gameObject.transform.position, -Vector3.forward), out LaserHit))
         {
             transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
             if (LaserHit.collider.tag == "Enemy")
             {
                 SetLaserEndpoint(gameObject.transform.position + (new Vector3(0, 0, -LaserHit.distance)));
             }
         }
         else
         {
             SetLaserEndpoint(gameObject.transform.position + (new Vector3(0,0,-10)));
         }
     }
 
     void SetLaserEndpoint(Vector3 point)
     {
         lineRenderer.SetPosition(1, point);
     }

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 Jason B · Aug 11, 2011 at 09:52 PM 0
Share

And as you can see, I even tried setting the rotation to 0,0,0 every time SetLength is called (about 10 times per second) so that I could rule out that it's somehow rotating. I also neglected setting the endpoint's X and Y values and locked them to 0 to ensure the endpoint doesn't misalign from the start point. Apparently does no good.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DoubleDouble · Aug 11, 2011 at 11:04 PM

I can only guess, but is your gameObject the parent in t$$anonymous$$s situation? you may want to make sure the transform.position you are using isn't local space.

If that isn't the problem, I would go through and print out just about every number in there to see where it goes wrong.

print(position/rotation/collider/laserEndpoint/etc.)

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
avatar image
0

Answer by Waz · Aug 11, 2011 at 11:28 PM

Set your LineRenderer to useWorldSpace. It is also odd that you are setting transform.position when there is a $$anonymous$$t - why it that going to matter, since your rays using global forward, not forward of the transform? (and I t$$anonymous$$nk you want Quaternion.identity, not the same t$$anonymous$$ng calculated from Eulers).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Surface with hole and Raycast - Which collider 1 Answer

physics2d.raycastall help please 1 Answer

Can requirecomponent specify IsTrigger on a collider? 1 Answer

AI sight script not detecting player 0 Answers


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