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
1
Question by NanoEngine · Jun 06, 2017 at 04:25 PM · raycastfpsgravitybullet

Simulating gravity to Raycast bullet

I know this question may be asked several times, but I couldn't find a proper solution. I'm currently developing a FPS game, and want to add a bullet gravity. I've considered using a rigidbody bullet, but I wanted to save some performance, so I decided to go with raycast. I've heard that it is possible to simulate the gravity effect, but I do not know how. I've thought about putting multiple small pieces of raycast together to simulate the raycast shooting. The problem here is though, it will have some framerate drops and expensive in performance. Another way that I've figured it out is to use a normal raycast(which goes forward), and lower the position depending on the distance once it hitted the object. But problem is that the player can not "predict" the bullet movement and shoot above the target, because the raycast wouldn't hit anything and return null.

How do you guys simulate gravity to raycast bullet? Any small codes to help me understanding will be great.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vyonox · Jun 10, 2017 at 09:05 AM

This is my solution for a different problem. Not the same because there is no gravity, but it could be applied to your solution:

  • My projectiles are fast (around 200m/s) but the distances are bigger (>500m).

  • There is no gravity, movement is a straight line. But I cannot just draw a line, the projectile sometimes needs to travel for 2 or 3 seconds to reach the target. That is more than 100 frames at 60fps.

  • I don't use rigidbody because there is no need. There are no physics involved in the projectile.

  • I don't use colliders because the projectile travels very fast. Discrete collisions would fail and continuous collision is overkill. I plan to have hundreds of projectiles on screen at the same time.

  • So, my current solution is to do a raycast every frame update. The raycast projects from current projectile position + (projectile speed) * (frame time). That is a mere 2 or 3 meters per frame update.

  • If the short raycast hits a collider, the projectile reports the hit. Otherwise, the projectile moves that short distance.

In your case, you can adjust the speed every frame update. Just add (gravity) * (frame time) to the speed.

Hope it helps.

Comment
Add comment · Show 3 · 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 NanoEngine · Jun 10, 2017 at 09:09 AM 0
Share

your solution sounds great, and it makes sense to me, but having a one trouble connecting the raycasts together. As far as I know, the raycast will not report anything unless it hits anything if(Physics.Raycast(...

Can you give me a little example on how you connected multiple raycasts together?

avatar image vyonox NanoEngine · Jun 10, 2017 at 10:29 AM 0
Share

Simple, move the bullet position in each fixed update. The next update, the raycast will start at current bullet position.

This is pseudo-code but easy to understand:

 void FixedUpdate()
 {
     // calculate new velocity (add gravity)
     // direction = velocity normalized
     // distance = velocity magnitude
     if (Physics.Raycast(position, direction, distance))
     {
          // Hit something
     }
     else
     {
         // No hit
         position += velocity;
     }
 }
avatar image NanoEngine · Jun 11, 2017 at 12:11 PM 0
Share

Your code helped me alot, but I'm having trouble calculating the velocity. Could you give me a quick example?

avatar image
0

Answer by Sanyika66 · Jun 10, 2017 at 07:45 AM

"Im absolute beginner" alert

Interesting question. As I know raycasts, they cant do that; just one straight line.. as I know. I see your problem about the "predicting" thing, thats okay. My only simple solution is: try to use standard rigidbodies again. It might be a bit more expensive, but if you navigate around the interface or code, you will probably see something that can help some reduction in performance requirements. You also said that this question is asked several times, i might recommend checking out the internet too, not just UA. You might find a video about it, or anything related. Sorry for no scripts or any better idea, im trying to help. If you managed to find a solution, please make it public, so other people can try it too! Also, if I helped, please tell me, that would be great.

Best regards, Alex

Comment
Add comment · Show 3 · 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 Sanyika66 · Jun 06, 2017 at 06:18 PM 0
Share

Check out these sites, might help you out: http://answers.unity3d.com/questions/349041/simulating-realistic-bullet-physics.html https://www.reddit.com/r/Unity3D/comments/3jerb7/simple_nonphysics_bullet_drop_with_raycasting/ Hope you understand all that chit-chat.

avatar image NanoEngine · Jun 10, 2017 at 08:26 AM 0
Share

You can not bend the raycast, but you can predict where the raycast should go in the beginning and start the raycast with a little bit of an angle. That way, you can actually implement gravity system.

avatar image NanoEngine · Jun 10, 2017 at 08:27 AM 0
Share

That's the solution I found so far.

And I seriously do not want to use rigidbodies, because my game is a multiplayer. Implementing gravity with raycast can be very easy, but the way how I do it, can be hard. I'm also trying to add trails to the raycast. $$anonymous$$aybe I did not ask my question clearly. I've checked out several websites you provided, but unexpectly, non of them really helped me. and I did find my solution by my self, and I just need time to check if it works or not. I'll notify in this thread, once i got it working.

avatar image
0

Answer by Cynikal · Jun 06, 2017 at 07:26 PM

You could always do a series of shorter raycasts, altering each ones rotation slightly.

Comment
Add comment · Show 1 · 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 NanoEngine · Jun 06, 2017 at 07:30 PM 0
Share

The problem in that method is though, the performance isn't good enough... But I would like to know how you would do the series of shorter raycasts. The thing is though, the raycast will go forever till it hits an object. Doesn't it (Even with the range, if the raycast does not detect(hit) anything, it won't do anything will it?)?

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

109 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

Related Questions

raycast not doing good 1 Answer

How to get bullets to hit crosshair 2 Answers

Make my gun model fire bullets 1 Answer

How can i make a raycast in my fps game? 1 Answer

Ray curve for bullet gravity effect, or any way to make it 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