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 /
avatar image
0
Question by Oyshoboy · Feb 06, 2018 at 01:08 AM · unity 5physicsraycastballistic

Is there way to curve Raycast?

Hi! Is tere any way to make a curved raycasts? I need it for shooting ballistics. Rigidbody bullets are not suitable, when speed is very fast - rigidobies could come through object without detecting.

So is there any ways to make it? Thank you!

Comment
Add comment · Show 2
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 bobisgod234 · Feb 06, 2018 at 03:13 AM 2
Share

You can use multiple casts to approximate a curve.

avatar image meat5000 ♦ · Feb 06, 2018 at 09:46 AM -1
Share

We did some tests a while back https://answers.unity.com/questions/1174027/object-moving-too-fast-so-that-the-collider-does-n.html

I dont know how much of it is still relevant in the latest Editions. The summary is that $$anonymous$$esh Colliders offer the best collision detection at high speeds.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BlueFire9020 · Jun 08, 2018 at 11:57 PM

I know this is kind of reviving a dead thread, but I was playing around with this and got something somewhat similar. It's by no means perfect, and could definitely use some improvement, but it does work and isn't that performance intensive. The iterations represents the scale of the curvature, as well as the scale of the ray. The Ray should have the same position as the startPos parameter and the Ray's direction is used to move the raycasts. You can tag things with a "Permeable" tag to get the raycast to pass through them (it only works once as I only included one secondary for loop, you can include more if you want to add more penetration). Like I said, it's not perfect but it could definitely be a start. Hope this helps! :D

 void curvedRaycast(int iterations, Vector3 startPos, Ray ray, int velocity)
     {
         RaycastHit hit;
         Vector3 pos = startPos;
         var slicedGravity = Physics.gravity.y / iterations / velocity;
         Ray ray2 = new Ray(ray.origin, ray.direction);
         print(slicedGravity);
         for (int i = 0; i < iterations; i++)
             {
                 if (Physics.Raycast(pos, ray2.direction * velocity, out hit, velocity))
                 {
                     Debug.DrawRay(pos, ray2.direction * hit.distance, Color.green);
                 if (hit.transform.tag == "Permeable")
                 {
                     Debug.DrawRay(pos, ray2.direction * velocity, Color.green);
                     pos += ray2.direction * velocity;
                     ray2 = new Ray(ray2.origin, ray2.direction + new Vector3(0, slicedGravity, 0));
                     for (int x = 0; x < iterations; x++)
                     {
                         if (Physics.Raycast(pos, ray2.direction * velocity, out hit, velocity))
                         {
                             Debug.DrawRay(pos, ray2.direction * hit.distance, Color.yellow);
                             return;
                         }
                         Debug.DrawRay(pos, ray2.direction * velocity, Color.magenta);
                         pos += ray2.direction * velocity;
                         ray2 = new Ray(ray2.origin, ray2.direction + new Vector3(0, slicedGravity, 0));
                     }
                 }
                 else
                 {
                     return;
                 }
             }
                 Debug.DrawRay(pos, ray2.direction * velocity, Color.cyan);
                 pos += ray2.direction * velocity;
                 ray2 = new Ray(ray2.origin, ray2.direction + new Vector3(0, slicedGravity, 0));
         }
         Debug.DrawRay(startPos, pos, Color.red);
         /*for (int i = 0; i < iterations; i++)
         {
             Debug.DrawRay(pos, ray2.direction * velocity, Color.red);
             pos += ray2.direction * velocity;
             ray2 = new Ray(ray2.origin, ray2.direction + new Vector3(0, slicedGravity, 0));
         }*/
     }

This is the experimental version I was working on to try and reduce the number of parameters. Only issue with this one is that the rays don't connect, despite the math and logging suggesting they should. If anyone figures out why and can fix it feel free to let me know!

EDIT: I updated and redid this script, it works 100% percent now!

 Vector3[] GravCast(Vector3 startPos, Vector3 direction, int killAfter, int velocity, bool reflect)
     {
         RaycastHit hit;
         Vector3[] vectors = new Vector3[killAfter];
         Ray ray = new Ray(startPos, direction);
         for (int i = 0; i < killAfter; i++)
         {
             if(Physics.Raycast(ray,out hit,1f))
             {
                 if (reflect)
                 {
                     print(hit.normal);
                     /*for (int e = 0; e < killAfter; e++)
                     {
                         if (Physics.Raycast(ray, out hit, 1f))
                         {
                             return vectors;
                         }
                         ray = new Ray(ray.origin + ray.direction, ray.direction + (Physics.gravity / killAfter / velocity));
                     }*/
                 }
                 return vectors;
             }
             Debug.DrawRay(ray.origin, ray.direction, Color.blue);
             ray = new Ray(ray.origin + ray.direction, ray.direction + (Physics.gravity / killAfter / velocity));
             vectors[i] = ray.origin;
 
         }
         return vectors;
     }
 }
 
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
avatar image
0

Answer by Xornogard · Feb 06, 2018 at 09:41 AM

Have you considered combining raycast with rigidbody? Each fixed update check raycast toward next position, if it hits, then you'll detect your object even when rigidbody has high velocity.

Comment
Add comment · Show 2 · 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 fafase · Feb 06, 2018 at 09:48 AM 1
Share

Not towards next position, but against previous. You don't know next position and if you wanted to know it you'd have to perform extra calculation that are not needed while checking behind.

avatar image b1gry4n · Jun 02, 2018 at 11:00 PM 0
Share

This is the method i would choose in this situation. have your bullet shoot a ray from its last position to its current position and check for collision that way. store the last position as the current position after this calculation fails.

alt text

f1 and f2 being the frames. solid red B being the current bullets position. dotted red B being the bullets last position.

2.jpg (20.8 kB)
avatar image
0

Answer by fafase · Feb 06, 2018 at 08:51 AM

You can make your bullet report whether something is between its current position and previous position.

 public class BulletCollision : MonoBehaviour
 {
     private Vector3 previous;
     public event Action<GameObject> RaiseCollision;
      void Start()
     {
           this.previous = this.transform.position;
     }
     void Update()
     {
           RaycastHit hit
           if(Physics.Linecast(this.previous, this.transform.position, out hit)){
                   if(RaiseCollision != null) { RaiseCollision(hit.collider.gameObject); }
           }
           this.previous = this.transform.position;
     }
 }


At this point, your projectile does not need collider anymore.

Comment
Add comment · Show 2 · 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 andrew-lukasik · Jun 04, 2018 at 07:57 AM 0
Share

Since Physics is synced with FixedUpdate this code should be executed there ins$$anonymous$$d of Update. Update is synced with rendering.

avatar image fafase andrew-lukasik · Jun 08, 2018 at 01:49 PM 0
Share

Would not make much difference here since it uses the position. Physics.Linecast is not related to actual Physics like Rigidbody.

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

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

Related Questions

raycast draw diagonal line problem? 1 Answer

Linecast, rotate end point relative to objects rotation 1 Answer

move a object hit by raycast 2 Answers

How to build a spaceship with interior? 1 Answer

Unity 2D Car Side Scroller question. 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