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 /
  • Help Room /
avatar image
0
Question by AmazingDeveloper2 · Feb 15, 2016 at 05:20 PM · c#unity 5linerendererdrawline

Making Raycast line visible

I want to make a visible line, where start point is fixed and end point can change.

I tried to make it with DrawRay. It's all fine, but I can see the line through the wall:

 void Update()
 {
     Vector3 forward = transform.TransformDirection(Vector3.forward) * 30;
     Debug.DrawRay(transform.position, forward, Color.green);
 }

alt text

Then I tried to use Line Renderer component, but I can't see the line at all..

alt text

How can I solve it?

423.jpg (39.0 kB)
234.jpg (38.9 kB)
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 LazyElephant · Feb 15, 2016 at 06:15 PM 0
Share

You have Use World Space checked on the line renderer, did you look near the world origin point for the line?

2 Replies

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

Answer by EmHuynh · Feb 15, 2016 at 08:33 PM

Thanks to, @Moaid_T4's script I was able to quickly whip up a laser script. It uses LineRenderer and the laser won't go through walls. Attach LaserScript.cs and LineRenderer to the player's game object.

Here is LaserScript.cs:

 using UnityEngine;
 using System.Collections;
 
 public class LaserScript : MonoBehaviour
 {
     public LineRenderer laserLineRenderer;
     public float laserWidth = 0.1f;
     public float laserMaxLength = 5f;
 
     void Start() {
         Vector3[] initLaserPositions = new Vector3[ 2 ] { Vector3.zero, Vector3.zero };
         laserLineRenderer.SetPositions( initLaserPositions );
         laserLineRenderer.SetWidth( laserWidth, laserWidth );
     }
 
     void Update() 
     {
         if( Input.GetKeyDown( KeyCode.Space ) ) {
             ShootLaserFromTargetPosition( transform.position, Vector3.forward, laserMaxLength );
             laserLineRenderer.enabled = true;
         }
         else {
             laserLineRenderer.enabled = false;
         }
     }
 
     void ShootLaserFromTargetPosition( Vector3 targetPosition, Vector3 direction, float length )
     {
         Ray ray = new Ray( targetPosition, direction );
         RaycastHit raycastHit;
         Vector3 endPosition = targetPosition + ( length * direction );
 
         if( Physics.Raycast( ray, out raycastHit, length ) ) {
             endPosition = raycastHit.point;
         }
 
         laserLineRenderer.SetPosition( 0, targetPosition );
         laserLineRenderer.SetPosition( 1, endPosition );
     }
 }
 

I hope this script helps, @dmitry.kozyr

Comment
Add comment · Show 2 · 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 AmazingDeveloper2 · Feb 15, 2016 at 08:49 PM 0
Share

Yes, it helped, thanks)

avatar image EmHuynh · Feb 15, 2016 at 09:01 PM 0
Share

Awesome! Let us know if you have more good questions. (:

avatar image
2

Answer by Moaid_T4 · Feb 15, 2016 at 07:00 PM

i really dont see a problem with the ray going through the wall since its a debug feature but non the less you will need to start a raycast (if you dont know what it is its a way to detect object/objects that are in a certian direction, the code will look something like this :

 void Update()
         {
             DetectHit(transform.position, 40, transform.forward);
         }
 
         RaycastHit DetectHit(Vector3 startPos, float distance,Vector3 direction)
         {
             //init ray to save the start and direction values
             Ray ray = new Ray(startPos, direction);
             //varible to hold the detection info
             RaycastHit hit;
             //the end Pos which defaults to the startPos + distance 
             Vector3 endPos = startPos + (distance * direction);
             if (Physics.Raycast(ray, out hit, distance))
             {
                 //if we detect something
                 endPos = hit.point;
             }
             // 2 is the duration the line is drawn, afterwards its deleted
             Debug.DrawLine(startPos, endPos, Color.green, 2);
             return hit;
         }
Comment
Add comment · Show 2 · 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 AmazingDeveloper2 · Feb 15, 2016 at 07:43 PM 0
Share

But I still can see it through the walls. I want to make something like a laser pointer. I want to make player see it, but when he will see it through the all walls in the scene, it will be incorrect. And actually I can see it only when Gismos option in game view enabled

avatar image EmHuynh AmazingDeveloper2 · Feb 15, 2016 at 08:14 PM 2
Share

Hello, @dmitry.kozyr. To help you get on track, I modified to @$$anonymous$$oaid_T4's script to fit your need. Attach the laser script and LineRenderer to the same game object. For the LineRenderer, set the size of its position to 2. With this script, the laser won't go through walls.

 using UnityEngine;
 using System.Collections;
 
 public class LaserScript : $$anonymous$$onoBehaviour
 {
     public LineRenderer laserLineRenderer;
     public float laser$$anonymous$$axLength = 5f;
 
     void Update () {
         laserLineRenderer.SetPosition( 0, transform.position );
         laserLineRenderer.SetPosition( 1, DetectHit( transform.position, laser$$anonymous$$axLength, Vector3.forward ) );
     }
 
     Vector3 DetectHit(Vector3 startPos, float distance,Vector3 direction)
     {
         Ray ray = new Ray(startPos, direction);
         RaycastHit hit;
         Vector3 endPos = startPos + (distance * direction);
 
         if( Physics.Raycast( ray, out hit, distance ) ) {
             endPos = hit.point;
             return endPos;
         }
 
         return endPos;
     }
 }
 

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

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

Related Questions

Draw a line in forward direction of a object 1 Answer

Unity 2D Touch drag specific Object 0 Answers

Suggest best hosting server for multiplayer Game??? 0 Answers

Accessing variable from another gameobject does not work 2 Answers

How to change Sprite Image when it reaches 90 degrees? 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