Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 RobbieH · Sep 30, 2013 at 03:25 PM · 2d-platformerlinerendererlaser

Line Renderer. How to lock z axis for side scroller?

I'm making a side scrolling platformer/shooter and want the player to fire a laser when the left mouse button is held and the laser to follow the mouse (twin stick shooter type controls). The line should origin from the players gun and to infinity, unless it hits collision or an enemy.

I sourced the following code, which I attached to the player, which sort of works for mockup visualisation, but if it doesnt hit any assets it wont render, and if there are assets on a different value along the z axis, i.e. background decorations, when I click the line will render from the player to that asset (forgive the indentation, I'm new to scripting and trying my best):

 #pragma strict
  
 @script RequireComponent (LineRenderer)
  
  
 var mouse : Vector2;
 var hit : RaycastHit;
 var range : float = 100.0;
 var line : LineRenderer;
 var lineMaterial : Material;
 var ray : Ray;
  
 function Start()
 {
 line = GetComponent(LineRenderer);
 line.SetVertexCount(2);
 line.renderer.material = lineMaterial;
 line.SetWidth(0.1f, 0.25f);
 }
  
 function Update()
 {
 ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 if(Physics.Raycast(ray, hit, range))
 {
 if(Input.GetMouseButton(0))
 {
 line.enabled = true;
 line.SetPosition(0, transform.position);
 line.SetPosition(1, hit.point );
 }
 else
 line.enabled = false;
 }
  
 }

I want the line to render regardless, and be locked to 0 on the z axis, the same as the player. Any help would be greatly appreciated. I have searched extensively for answers but not found anything specific.

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Sep 30, 2013 at 03:32 PM

On line 30, insert:

 hit.point.z = 0.0;

Or alternately if you are going to use hit.point;

 var v3 = hit.point;
 v3.z = 0.0;
 line.SetPosition(1, v3);
Comment
Add comment · Show 9 · 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 RobbieH · Sep 30, 2013 at 04:12 PM 0
Share

Thanks for replying. If I change line 30 to:

line.SetPosition(1, hit.point.z = 0.0 );

I get a compile error.

Alternatively, if I add:

var v3 = hit.point; v3.z = 0.0;

and change line 30 to:

line.SetPosition(1, v3);

Then my laser will project straight up with a finite range.

Could you possibly show me in context, if I have done it wrong? (I can confirm my world co-ords are set up with z as the depth).

avatar image robertbu · Sep 30, 2013 at 04:54 PM 0
Share
 #pragma strict
  
 @script RequireComponent (LineRenderer)
  
 var mouse : Vector2;
 var hit : RaycastHit;
 var range : float = 100.0;
 var line : LineRenderer;
 var line$$anonymous$$aterial : $$anonymous$$aterial;
 var ray : Ray;
  
 function Start()
 {
     line = GetComponent(LineRenderer);
     line.SetVertexCount(2);
     line.renderer.material = line$$anonymous$$aterial;
     line.SetWidth(0.1f, 0.25f);
 }
  
 function Update()
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit, range))
     {
         if(Input.Get$$anonymous$$ouseButton(0))
         {
             line.enabled = true;
             line.SetPosition(0, transform.position);
             hit.point.z = 0.0;
             line.SetPosition(1, hit.point );
         }
         else
             line.enabled = false;
         }
 }
avatar image RobbieH · Sep 30, 2013 at 07:05 PM 0
Share

That works brilliantly. Thank you.

FAO others looking for this solution:

It will still not render if there is nothing for the cursor to hit, but that can be solved temporarily by placing a plane in the background. Also the line will pass through collision unless the cursor is over the actual collision being aimed at. But for the purpose of my question - robertbu has solved it.

avatar image robertbu · Sep 30, 2013 at 08:36 PM 0
Share

Note you can solve the "not render if there is nothing for the cursor to hit" issue by detecting when it does hit, establishing a default distance and using Camera.ScreenToWorldPoint() to place the point.

avatar image RobbieH · Oct 01, 2013 at 06:04 AM 0
Share

Thanks. I'm assu$$anonymous$$g the default distance would be 0 along the z axis in world space. I just need to work out how to implement it, like I said I'm still getting my head around scripting, but I n eed to experiment. $$anonymous$$y initial reaction would be to replace Camera.main.ScreenPointToRay with Camera.ScreenToWorldPoint, but the lack of Ray makes me think it will be an issue. I'll try after work.

Show more comments

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

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

Related Questions

LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer

LineRenderer not working 0 Answers

LineRendering Forward 1 Answer

Creating a Ray Gun 1 Answer

Laser using line renderer 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