Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Althaen · Nov 16, 2012 at 04:35 AM · c#raycasts

Simulating realistic bullet physics.

I am developing a FPS game and I want to have bullets drop over distance and actually take time to travel but I don't want to use rigidbodies because they're too unreliable for detecting collisions.

In my head I have thought maybe I could take the angle that the weapon fires at and use that with an algorithm of some sort that will give me points along the trajectory that I can fire rays at different angles along with collision detection. I'm unsure however how I could accomplish this.

Could I have the rays fire at different points and time until there is a collision detected so the bullet doesn't travel instantly but also not miss any collisions?

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

2 Replies

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

Answer by Owen-Reynolds · Nov 16, 2012 at 04:51 AM

That's basic calculus (which means it's not really calculus.) Estimate the curve by doing lots of little straight lines. The official math term for this is the "shooting method."

The "bullet" would be just a set of numbers -- position, and x,y,z speed. Maybe spawn a bullet empty with that script. Each frame move the bullet forward and add gravity, then raycast from start to end. Then decide you want to do that in three steps/per frame (move 1/3rd, add 1/3 gravity, three times.)

If you do it adding gravity before the move (always a little too low,) then again adding after (always a little too high,) you bracket the real value, so can play with steps/frame until the error is tiny.

May as well add the wind, as long as you're doing all that work.

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 Althaen · Nov 16, 2012 at 05:10 AM 0
Share

Oh god not calculus.

Could I fire a ray like 50 metres and then spawn an empty at the end and rotate it downwards and fire another ray and do this until there's a collision?

What about using a rigidbody and capturing it's current position every so frames and fire a ray to detect collisions?

avatar image Owen-Reynolds · Nov 16, 2012 at 02:34 PM 0
Share

There's no need to spawn a trail of ray end-points. You can just have a `bulletPos` variable, with no backing gameObject. and raycast from `bulletPos` in direction`bulletSpd` for distance `bulletSpd.magnitude`. Then set `bulletSpd.y+=grav;` and `bulletPos+=bulletSpd;` and repeat.

You might want to test-spawn tiny no-collider spheres at each point, just to check that the curve looks O$$anonymous$$.

avatar image
0

Answer by Major · Jun 20, 2017 at 05:23 AM

Post is old, but somebody might find this useful! Just apply the script to a spawned GameObject, and everything else will be taken care of.

 using UnityEngine;
 using System.Collections;
            
             public class BulletPhysics : MonoBehaviour {
                 public float gravity = -9.81f;
                 public float velocity = 350;
                 public float maxTime = 5f;
    
                 private Vector3 origin; 
                 private Vector3 cameraDir;
                 private float increment = 0;
            
                 void Start(){
                     origin = transform.position;
         cameraDir = Camera.main.transform.forward;
                 }
            
                 void FixedUpdate () {
                     //displacement = v0*t + 0.5at^2 + origin
                     //Debug.DrawLine (disp(increment), disp(increment + 0.02f), Color.red, 10);
                     RaycastHit hit;
            
                     if (Physics.Linecast(disp(increment), disp(increment + 0.02f), out hit)) {
                         Destroy (gameObject);
                     }
            
                     if (increment > maxTime)
                         Destroy (gameObject);
            
                     transform.position = disp (increment + 0.02f);
                     increment += 0.02f; //<--- fixedupdate time (value in Time settings)
                 }
            
                 Vector3 disp (float t){
                     Vector3 vel = velocity * cameraDir;
                     Vector3 disp = origin + vel * t - (playerDown * Mathf.Pow (t, 2) * gravity * 0.5f - Vector3.up * t);
                     return disp;
                 }
             }

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 Blastericko · Oct 04, 2017 at 04:34 PM -1
Share

can I ask what is playerDown?? :D

avatar image Major Blastericko · Oct 04, 2017 at 04:44 PM 0
Share

Looks like I didnt fully fix this version, whoops.

playerDown is a unit vector thay denotes the relative "down" direction for the player. For any normal shooter senario this is equivilant to Vector3.down.

avatar image Game1324 · Oct 09, 2020 at 01:07 AM 0
Share

Oh my god you are my hero thank you my god

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Rotating an Object to normal 0 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