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 kerkzhan · Apr 23, 2018 at 09:00 AM · raycastraycastingbulletlinecast

Ricochet Problem of projectile bullets

Hi everyone, I'm having trouble implementing bullets that ricochet a certain amount of times.


PROBLEM

1) The direction of the reflection is correct, but for some reason, it reflects in a wrong direction briefly before changing to the correct reflection direction. (I do notice if I lower the bullet speed it will reflect correctly, so it has something to do with the line casting, but I can't wrap my head on the specific problem)


2) The reflection only occurs once, despite the reflections variable being at 100. I have checked and the Line casting does collide with the second wall, but the reflection does not occur.


3) It's not in the video, but sometimes my projectiles pass through walls. Is this a problem of the Linecast not being able to detect it? I thought Line cast was supposed to overcome this problem.

Here is a video that indicates the problems above.


My friend really wants to have trails for the bullets, so projectiles + trail renderer is what I'm using.


I did think of alternatives using Raycasts to detect the richochet points and for collision, and have my projectile bullets follow the raycast path until it reaches the hit.point, then reflecting it. (Just for the trails).


But then again it's the same thing because the bullets will be too fast to even land on that hit point.


This is the script attached to the bullet.


 void FixedUpdate () {
 
         RaycastHit hit;
             
         if (Physics.Linecast (previousPosition, this.transform.position, out hit)) {
             if (hit.transform.gameObject.tag == "Target") {
 
                 if (reflections == 0) {
                     return;
                 }
 
                 Vector3 newDirection = Vector3.Reflect (this.transform.forward, hit.normal);
 
                 this.transform.position = hit.point;
                 rb.velocity = newDirection * bulletSpeed;
 
                 reflections--;
             }
         }
         previousPosition = this.transform.position;
     }

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 Harinezumi · Apr 23, 2018 at 01:32 PM 0
Share

This sounds like a problem with physics simulation, as if the position of the projectile were corrected after the collision. And sure enough, that's exactly what the code does: you modify the position to be the hit point, and then modify the velocity of the rigidbody.
Both of these operations are recommended to be avoided, because they are inefficient and create unrealistic behaviour (see documentation). If you really need to modify the position of a rigidbody, use rigidbody.position (for some reason it is 10-20 times faster).
Also, your projectile is fast enough to penetrate into colliders (that's why it doesn't miss objects when you lower its speed). Setting the rigidbody's Collision detection property to Continuous should help with this.

Of course, none of this solves your actual problem. I would just try to use the normal physics engine to reflect the projectile, after setting a 100% bouncy and 0 friction physics material, and drag set to 0 and gravity disabled. Have you tried this?
And if this doesn't work, you can also try manually simulating with raycasts and interpolation, and setting the rigidbody to kinematic.

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

116 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

Related Questions

Multiple linecasts question 1 Answer

Multiple raycasts in same fixedupdate 3 Answers

Linecast blocked by colliders not actually in the way? 0 Answers

Help with Raycast C# 0 Answers

Simulating Bullet Gravity using Raycasting 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