Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 G_hi3 · Jun 24, 2021 at 06:20 PM · raycastphysics2dphysics2d.raycast

Raycast ricochet only works for a couple of Rays

I'm working on an AI script that can sense the player and shoot them using ricocheting bullets. To achieve this, I use a script that casts rays in all directions that collide with walls. Raycasting to the walls works great and I get some nice ricocheted rays, but most of them don't ricochet at all and I can't figure out why.

 using UnityEngine;

 public class TargetFollowing : MonoBehaviour
 {
   [SerializeField] private LayerMask _layerMask;
   [SerializeField] private int _numberOfRays;
   [SerializeField] private float _raycastDistance;
   private Transform _transform;

   private void Awake() {
     _transform = GetComponent<Transform>();
   }

   private void Update() {
     for (var i = -_numberOfRays / 2; i < _numberOfRays / 2; i++) {
       var direction = GetRelativeDirection(new Vector3(Mathf.Sin(i), Mathf.Cos(i), 0f));
       CastRay(_transform.position, direction, 3);
     }
   }

   private Vector3 GetRelativeDirection(Vector3 originalDirection) {
     return _transform.TransformDirection(originalDirection);
   }

   private void CastRay(Vector3 currentPosition, Vector3 rayDirection, int numberOfRicochets) {
     var raycastHit = Physics2D.Raycast(currentPosition, rayDirection, _raycastDistance, _layerMask.value);
     if (raycastHit.collider == null) {
       Debug.DrawRay(currentPosition, rayDirection * _raycastDistance, Color.red);
       return;
     }
     var targetPosition = rayDirection * raycastHit.distance;
     Debug.DrawRay(currentPosition, targetPosition, GetRaycastColor(numberOfRicochets));
     if (numberOfRicochets <= 0) {
       return;
     }
     var ricochetDirection = Vector2.Reflect(rayDirection, raycastHit.normal);
     CastRay(raycastHit.point, ricochetDirection, numberOfRicochets - 1);
   }

   private static Color GetRaycastColor(int ricochetNumber) {
     switch (ricochetNumber) {
       case 0: return Color.cyan;
       case 1: return Color.green;
       case 2: return Color.yellow;
       case 3: return Color.magenta;
       default: return Color.black;
     }
   }

 }

The values of the serialized properties I used in the screenshots at the bottom are:

  • Layer Mask: only Walls

  • Number Of Rays: 45

  • Raycast Distance: 100

Playing around with the raycast distance didn't really help either.

alt text alt text

many-raycasts.png (47.9 kB)
image-2021-06-24-201422.png (33.5 kB)
Comment
Add comment · Show 1
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 bdubbert · Jun 24, 2021 at 09:22 PM 0
Share

Nothing jumps out at me on first read. My suggestion would be to go down to just 1 ray and find a case where it doesn't bounce, then step through your code and find out where it is breaking down

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by G_hi3 · Jun 24, 2021 at 09:24 PM

I figured out the problem in my script by displaying the normals of each raycast collision. First, every collision that didn't ricochet had two normal directions. This indicated to me, that there was a raycast from and to that exact point. Then I used ~Physics2D.RaycastAll` to find all collisions and proceeded to print out the distance of each hit for every raycast with multiple collisions. The Console was filled with exponentially more "0" than other distances. I updated my code with this at the top of my CastRay method:

 var allRaycastHits = Physics2D.RaycastAll(currentPosition, rayDirection, Mathf.Infinity, _layerMask.value);
 var raycastHit = allRaycastHits.FirstOrDefault(h => h.distance > float.Epsilon);
 if (raycastHit == default) {
   Debug.DrawRay(currentPosition, rayDirection * Mathf.Infinity, Color.red);
   return;
 }

So basically, give me the first collision that isn't the point of origin and use that to display the ricochet.

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

170 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

Related Questions

Physics2D Raycast not detecting wall colliders 1 Answer

2D Raycast Layer Mask not working 1 Answer

Is it possible to raycast during edit mode? 1 Answer

Can't find the null reference 1 Answer

Bitmask/LayerMask used in raycast not working 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