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 /
  • Help Room /
avatar image
0
Question by DanielDouglas · May 23 at 09:01 AM · hit

LineRender, Raycast, Laser point A to Point B

my object is create a line from point A to point B, in point B (hit.point) i need to create a new line from point B to point C, but maintein de line before A-B. the new line need to give the color from material hit. like image:alt text

my code:

using UnityEditor; using UnityEngine;

public class BeamerManager : MonoBehaviour { [Header("Laser Controls")]

 [SerializeField] private GameObject LaserPrefab;
 [SerializeField] private Transform firePoint;
 [SerializeField] private float maxLenght;
 [SerializeField] private int reflections;

 //Laser Attributes

 private Renderer laserMaterialModel;
 private LineRenderer lineRenderer;
 
 private Ray ray;
 private RaycastHit hit;
 private float remainingLenght;
 private Color defaultColor;


 private void Awake()
 {
     

 }

 void Start()
 {
     laserMaterialModel = LaserPrefab.GetComponent<Renderer>();
     lineRenderer = LaserPrefab.GetComponent<LineRenderer>();
     defaultColor = laserMaterialModel.material.color;
 }
 void OnDrawGizmos()
 {
     Handles.color = Color.red;
     Handles.ArrowHandleCap(0, firePoint.position, firePoint.rotation, 0.5f, EventType.Repaint);
     DrawGismoLine(firePoint.position + firePoint.forward * 0.75f, firePoint.forward, reflections);
 }
 void Update()
 {
     UpdateLaser();
 }

 private void UpdateLaser()
 {
     
     ray = new Ray(firePoint.position, firePoint.forward);
     lineRenderer.positionCount = 1;
     lineRenderer.SetPosition(0, firePoint.position);
     remainingLenght = maxLenght;
     if (!Physics.Raycast(ray, out hit, remainingLenght))
     {
         lineRenderer.positionCount += 1;
         lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLenght);
         laserMaterialModel.material.SetColor("_Color", defaultColor);
     }
     else
     {
        DrawPredictedReflectionPattern(firePoint.position, firePoint.forward, reflections, null);
     }
 }
 private void DrawPredictedReflectionPattern(Vector3 position, Vector3 direction, int reflectionsRemaining, GameObject newLine)
 {
     if (reflectionsRemaining == 0)
     {
         return;
     }

     
     //Vector3 startPosition = position;
     ray = new Ray(position, direction);
     
     //verify hit something
     if (Physics.Raycast(ray, out hit, remainingLenght))
     {
         //verify "Sphere" hit or not
         if (hit.collider.tag == "Sphere")
         {
             if ( newLine != null)
             {
                 print("Destruiu");
                 Destroy(newLine);
                
                 //lineRenderer.positionCount = 0;

             }
             lineRenderer.positionCount += 1;
             lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
             newLine = Instantiate(LaserPrefab);
             lineRenderer = newLine.GetComponent<LineRenderer>();
             laserMaterialModel = newLine.GetComponent<Renderer>();
             direction = ray.direction;
             position = ray.origin + ray.direction * remainingLenght;
             lineRenderer.positionCount = 1;
             lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);

            

             print("Entrou na Sphere - " + hit.collider.tag);
             SetLaserColor();
             //print("");

         }
         else
         {
             if (newLine != null)
             {
                 Destroy(newLine);
                 lineRenderer = null;
                 //lineRenderer.positionCount = 0;
                 print("Destruiu");
             }
             newLine = Instantiate(LaserPrefab);
             lineRenderer = newLine.GetComponent<LineRenderer>();
             direction = Vector3.Reflect(direction, hit.normal);
             position = hit.transform.position;
             //SetLaserColor();
         }
         
     }
     else
     {
         position += direction * maxLenght;
     }

     
     lineRenderer.positionCount += 1;
     lineRenderer.SetPosition(lineRenderer.positionCount - 1, position);
     DrawPredictedReflectionPattern(position, direction, reflectionsRemaining - 1,newLine);
 }

 private void DrawGismoLine(Vector3 position, Vector3 direction, int reflectionsRemaining)
 {
     if (reflectionsRemaining == 0)
     {
         return;
     }

     Vector3 startingPosition = position;

     ray = new Ray(position, direction);

     //verify hit something
     if (Physics.Raycast(ray, out hit, maxLenght))
     {
         //verify "Sphere" hit or not
         if (hit.collider.tag == "Sphere")
         {

             direction = ray.direction;
             position = hit.collider.gameObject.transform.position;
             

         }
         else
         {
             direction = Vector3.Reflect(direction, hit.normal);
             position = hit.point;

         }

     }
     else
     {
         position += direction * maxLenght;
     }

    
     Gizmos.color = Color.magenta;
     Gizmos.DrawLine(startingPosition, position);

     DrawGismoLine(position, direction, reflectionsRemaining - 1);
 }
 private void SetLaserColor()
 {
     //verify if get a Component Renderer form hit object
     if (hit.transform.GetComponent<Renderer>())
     {

         Renderer selectionRenderer = hit.transform.GetComponent<Renderer>();
         Material hitMaterial = new Material(selectionRenderer.material);

         Color hitColor = new Color(hitMaterial.color.r, hitMaterial.color.g, hitMaterial.color.b, hitMaterial.color.a);
         hitColor.a = 100;

         laserMaterialModel.material.SetColor("_Color", hitColor);
     }
 }
 //private void OnDestroy()
 //{
 //    Destroy(laserMaterialModel.material);
 //}

}

exemplo.png (4.3 kB)
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

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

183 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 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

How can i detect 2 raycasthit2D in the same function storing their Vector2 position? 0 Answers

How to create reel/slider with mobile controls 0 Answers

get array of RaycastAll hits 1 Answer

Newbie Simple Melee 1 Answer

Tennis Ball Hit differents effects and take parabola 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