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
2
Question by ChrisJoosten · Mar 05, 2014 at 02:27 PM · raycastlinerenderer

Reflecting linerenderer with raycast?

Hi, im trying to make a "laser" reflect against objects. But I don't know how i can make it to change direction when It's hitting something. When I try to change the direction of the "laser" It changes it at the Startposition and not at the Hit position.

This is my code so far. using UnityEngine; using System.Collections;

 public class testLaserScript : MonoBehaviour {
 
     LineRenderer lineRenderer;
 
     void Awake () {
         lineRenderer = GetComponent<LineRenderer> ();
     }
 
     void Start(){
 
     }
     
     void Update () {
         Laser ();
     }
 
     void Laser(){
         RaycastHit hit;
         if(Physics.Raycast(transform.position,Vector3.right,out hit,Mathf.Infinity)){
             lineRenderer.enabled = true;         
             lineRenderer.SetPosition(0, transform.position); 
             lineRenderer.SetPosition(1, hit.point);   
 
 
         }
     }
 }
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

2 Replies

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

Answer by ChrisJoosten · Mar 06, 2014 at 12:53 PM

I got a little progression

 using UnityEngine;
 using System.Collections;
 
 public class testLaserScript : MonoBehaviour {
 
     LineRenderer lineRenderer;
 
     void Awake () {
         lineRenderer = GetComponent<LineRenderer> ();
     }
 
     void Start(){
 
     }
     
     void Update () {
         Laser ();
     }
 
     void Laser(){
         RaycastHit hit;
 
 
         if(Physics.Raycast(transform.position,Vector3.forward,out hit,Mathf.Infinity)){
 
             lineRenderer.enabled = true;         
             lineRenderer.SetPosition(0, transform.position); 
             lineRenderer.SetPosition(1, hit.point);   
 
             if(hit.collider.tag == "Mirrors"){
                 Vector3 pos = Vector3.Reflect (hit.point - this.transform.position, hit.normal);
                 lineRenderer.SetPosition(2,pos);
                 //lineRenderer.SetPosition(3, pos);
             }
         }
     }
 }


With this code it reflects against 1 object with the tag "Mirrors". No i need to make something it reflects against mutliple objects. It has to be dynamicle so when i remove a "Mirrors" object from the scene It doesn't bug of give an error.

Thanks in regards!

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 VinnyAmbesek · Nov 05, 2015 at 12:26 AM 0
Share

I am trying to do the same thing. When I use your code the laser gets smaller close to the mirrors and grows again as soon as it departs in another direction. Any ideas why?alt text

captura-de-tela-2015-11-04-212507.png (94.1 kB)
avatar image
-1

Answer by CatsPawGames · Sep 10, 2014 at 03:55 AM

This package contains ray visualization including complex reflections using colliders:

https://www.assetstore.unity3d.com/en/#!/content/18445

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

How can I can I cast a ray from a gameobject? 1 Answer

Line Renderer Issues 1 Answer

Setting Line Render position directly forward from facing of game object 1 Answer

Shorten Raycast - Line Renderer 3 Answers

How would I set the end of my line renderer to the first collision point with a raycast? 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