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 bayernsa · Oct 24, 2014 at 02:21 PM · javascriptcollisiondetectionline renderer

Line Renderer hit detection?

 @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(1))
             {
                 line.enabled = true;
                 line.SetPosition(0, transform.position);
                   line.SetPosition(1, hit.point + hit.normal);
               }
               else
               line.enabled = false;
         }
 
 }

this isnt my script but im trying to modify this. at the moment the line goes through all objects, which isnt what i want. what would i need to add to stop the line going through objects. (currently my camera is setup for isometric) thanks in advance.

Comment
Add comment · Show 6
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 MrSoad · Oct 24, 2014 at 02:37 PM 0
Share

This is a script from a pool game I made, it is drawing a line from the player ball to wherever it hits in the raycast, this works so you should be able to adapt it.

 function subDraw_Aim() {
 
     var hit : RaycastHit;
     
     if (Physics.Raycast (transform.position, -transform.forward, hit)) {
         oAim_LineRenderer.SetPosition(0, transform.position);
         oAim_LineRenderer.SetPosition(1, hit.point);
     }
 }

oAim_LineRenderer is my line renderer object.

avatar image Andrewlo3 · Oct 24, 2014 at 02:50 PM 0
Share

A few lines from one of my scripts.

 public void Shoot () {
     RaycastHit hit;
     float shotDistance = hit.distance;
 
     if (Physics.Raycast(ray, out hit, shotDistance)) {
         shotDistance = hit.distance;
     }
 }
avatar image MrSoad · Oct 24, 2014 at 03:01 PM 0
Share

Andrewlo3 is using C# by the look of it, and you are using js. $$anonymous$$y example is js so will prob suit you better.

avatar image fafase · Oct 24, 2014 at 03:28 PM 0
Share

The problem is not the language, more likely the result and the clarity. Though your comment (which should be an answer btw) may help, I would not think it solves entirely since it lacks some explanation. You cast from the object towards the -forward, so you need to rotate the object properly first. I guess because you were using the direction of the queue in your pool game, that worked.

Here it would be to cast from the object to the hit point of the camera ray.

  var hitPoint:Vector3;
  ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  if(Input.Get$$anonymous$$ouseButton(1) && Physics.Raycast(ray, hit, range))      
  {
       hitPoint = hit.point;
  }

  if(Physics.Linecast(transform.position, hitPoint, hit))
  {
      // Colliders in between
      // Draw from transform to hit.point
  }else{
      // No colliders in between
      // Draw from transform to hitPoint
  }

What you do is take the hit from the camera cast, then try to join the object to that point with a linecast, then if there are a hit, it returns true and you draw from your object to the hit, if free all the way you draw from object to camera hit.

(This should be an answer...)

avatar image Andrewlo3 · Oct 24, 2014 at 03:33 PM 0
Share

@fafase I left $$anonymous$$e as a comment because I knew it was not a copy paste solution, but rather a good starting point.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Collision 1 Answer

My Javascript collision isn't being detected, please help. 0 Answers

Trouble with raycast hitting a tagged object 0 Answers

Collision isn't detected between two obects 1 Answer

Spawning and health script not working with collider? 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