• 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
Question by gableonardo · Aug 21, 2017 at 03:45 PM · scripting problemcollisiongamereflectionslaser

Issue with Dynamic Laser Reflection on Walls

Hello, community!

I'm kind of a begginer on scripting and I'm making some sort of billiard game.

I'm having troubles with the reflection of the "power line" on the table walls, being the "power line" the line that's shown when you charge your launch in-game and the "table walls" the colliders that will reflect the shot.

To get you guys underway, the laser should work like this:

  • The power line is casted from a place called "LaserSpawn";

  • Whenever I click somewhere inside the game screen and hold the mouse button, the power line should be shown, and it's complete width (untill now) is equal to the width from the Laser Spawn to the mouse position, and it must go in the opposite direction; also the power line updates itself every frame the mouse button is hold;

  • I'm casting a raycast2D from the Laser Spawn and, as soon as it collides with a wall, a new raycast is casted from that point in the reflected direction with a calculated width between the first raycast's end point and the collision point with the wall;

  • Finally, if the new ray collides with another wall, the process should repeat: a new raycast should be casted from the next collision point, etc.

By now, I'm keeping the Line Renderer unabled, I'd rather make the raycast work first so that i guess I'll be able to make the line renderer propperly work next.

The thing is, when I set up the game to reflect the power line just ONE TIME it works perfectly, here's a sample:

alt text

But when I set it to reflect MORE THAN ONE TIME, the reflections seem to happen, but they start flickering. Because I can't upload more than two images, here's only a sample of the non-reflected line, it should be reflecting in the next wall:

alt text

Here's my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DirectionLineModified : MonoBehaviour
 {
     public Transform laserSpawner;
     public LayerMask _layerMask;
     LineRenderer line;
 
     void Start()
     {
         if(gameObject.GetComponent<LineRenderer>() != null)
         {
             line = gameObject.GetComponent<LineRenderer>();
         }
 
         for(int i = 0; i < line.positionCount; i++)
         {
             line.SetPosition(i, new Vector3(laserSpawner.position.x, laserSpawner.position.y, 0));
         }
     }
 
     void Update()
     {
 
         if (Input.GetMouseButton(0))
         {
             line.enabled = true;
             DrawLine(laserSpawner.position, new Vector3((2 * laserSpawner.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)).x, (2 * laserSpawner.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)).y, 0), 1);
         }
 
         else
         {
             for (int i = 0; i < line.positionCount; i++)
             {
                 line.SetPosition(i, new Vector3(laserSpawner.position.x, laserSpawner.position.y, 0));
             }
             line.enabled = false;
         }
 
     }
 
     void DrawLine(Vector2 initRayPos, Vector2 lastRayPos, int linePosIndex)
     {
         if (linePosIndex < line.positionCount)
         {
             RaycastHit2D hit = Physics2D.Raycast(initRayPos, lastRayPos - initRayPos, Vector3.Distance(lastRayPos, initRayPos), _layerMask);
 
             if (hit.collider != null)
             {
                 if (hit.collider.tag == "FalseWalls")
                 {
                     float remDist = Vector3.Distance(lastRayPos, initRayPos) - hit.distance;
                     Vector2 refVect = remDist * Vector2.Reflect((hit.point - initRayPos), hit.normal).normalized;
                     DrawLine(hit.point, refVect + hit.point, linePosIndex + 1);
                 }
 
             }
   
             Debug.DrawRay(initRayPos, lastRayPos - initRayPos);
         }
         
     }
 }


I've been trying for like three weeks and I can't seem to make it work, send help please :(

untitled2.png (136.0 kB)
untitled.png (146.9 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by SneakyLeprechaun · Aug 22, 2017 at 02:54 AM

I GOT IT!!!

So after duplicating your scene, I played around a bit with your scene. After a lot of testing, I discovered that the hit.normals weren't consistently facing the desired direction. I looked this up online, and found this post, which basically says not to use hit.point when Drawing the line. So what I did then was change DrawLine(hit.point, refVect + hit.point, linePosIndex + 1); to DrawLine(hit.point + new Vector2(refVect.x * 0.1f, refVect.y * 0.1f), refVect + hit.point, linePosIndex + 1); which just offsets the initRayPos by a little bit. If it's too big, you can just downscale the 0.1f value.

This solution worked for me, I hope it works for you as well!

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Contacts not registering? 0 Answers

How to make a GetKeyUp action to wait for a few seconds before activating 1 Answer

Wall Jump with Jump Delay 0 Answers

Doodle Jump Game Platform Spawn Issue Unity2D 0 Answers

Countdown Timer 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