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 Discipol · Apr 24, 2016 at 02:23 PM · raycastraycastingraycasthitraycasthit2dreflect

Need help ironing out this weird error in Raycast2D and Vector2.Reflect, for a billiard game test

I combed the entire internet looking for an answer but couldn't fix it, and I have been at it for a week. I have made a loop that fires a Raycast2D from the position of the gameobject with the reflecting script and it shoots to its transform.right (irrelevant). It reflects from Box Colliders 2D (I have several in the scene called "New Sprite" and clones).

The problem is that, if I move or rotate the origin, at one point or another, it reflects inside that same point over and over. You can see in the "bad" screenshots that after a point or another, its the same values for the line renderer. Sometimes it works good, other times it doesn't work after the first collision. The first collision is always good.

Goodalt text

Bad alt text

Here is the code for the bouncing script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent( typeof( LineRenderer ) )]
 public class BouncingLaser : MonoBehaviour
 {
     private LineRenderer laserRenderer;
 
     private int hitCount = 0;
 
     // Use this for initialization
     void Start()
     {
         laserRenderer = GetComponent<LineRenderer>();
         laserRenderer.SetWidth( 0.03f, 0.03f );
         laserRenderer.SetVertexCount( 1 );
     }
 
     public static Vector2 xy( Vector3 v )
     {
         return new Vector2( v.x, v.y );
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         //reset;
         hitCount = 0;
 
         Vector3 origin = transform.position;
         Vector3 direction = transform.right;
 
         Debug.Log( "-----------" );
         ExtractReflection( origin, direction );
     }
 
     void ExtractReflection( Vector3 origin, Vector3 direction )
     {
         direction = direction.normalized;
 
         if( hitCount == 6 )
         {
             return;
         }
 
         Debug.Log( hitCount + " - " + origin + " - " + direction );
 
         hitCount++;
         laserRenderer.SetVertexCount( hitCount );
         laserRenderer.SetPosition( hitCount - 1, origin );
 
         RaycastHit2D laserHit = Physics2D.Raycast( origin, direction * 10 );
 
         Debug.DrawRay( origin, direction, Color.black );
 
         if( laserHit.collider != null )
         {
             Vector2 reflectPos = Vector2.Reflect( direction, laserHit.normal );
 
             ExtractReflection( laserHit.point, reflectPos );
         }
         else
         {
 
             Debug.Log( "No hit at attempt: " + hitCount );
 
         }
     }
 }


good.png (146.6 kB)
bad-1.png (328.1 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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Oliver-Bogdan · Apr 24, 2016 at 07:09 PM

This happens because the new origin of the Physics2D.Raycast is inside the object that you previously found a collision with(right on the edge actually). laserHit.fraction will return 0 for this case.

Additionally, this will also detect Collider(s) at the start of the ray. In this case the ray is starting inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

You can fix this in multiple ways. A quick and VERY dirty hack: move the new raycast origin outside the already tested object.

 ExtractReflection(Vector3.Lerp(origin, laserHit.point, 0.98f), reflectPos );

Also keeping this in FixedUpdate rather than Update will calculate it multiple time per frame and this may not be what you desire.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RayCast2D and RayDraw errors 0 Answers

I can't get RayCast to report the collider I want. 1 Answer

Is there any way how to do raycasts like this? 0 Answers

Detect when Raycast DOESN'T hit layer. 1 Answer

Raycast hits Ray origin, possibly, randomly. 2 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