Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Raycasting with Bulletdrop?

Discussion in 'Scripting' started by Glemau, Jan 27, 2016.

  1. Glemau

    Glemau

    Joined:
    Jul 24, 2015
    Posts:
    37
    Hi,

    Can I somehow create a Ray, that goes downwards, like bulletdrop?
    Or How would you simulate this? Because i don't think shooting objects wouldn't work that well.

    ~Glemau
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    upload_2016-1-27_18-29-38.png

    basic vector math, red + yellow = green.

    red being the normal forward direction you would raycast without drop, yellow being a downward vector (-Vector3.up)... doesn't curve, but you're checking every frame so distances are going to be fairly small
     
  3. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Im going to bump this as i'd also like to know how to go about doing this , and the answer above doesn't even come close to answering the question , no offense.
     
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Bullet drop is the effect of gravity on a bullet and it doesn't form a a straight light, its more like a Parabola. Raycasts are straight lines so you are never going to get a perfect bullet drop using them.

    You could approximate bullet drop with raycasts by splitting the bullet drop into a collection of smaller raycasts, rotating the raycast direction each time simulating the effect. Starting each raycast from the end point of the pervious one. The more raycasts you use the more of an accurate result you will get.

    That said, you would just be better actually simulating a projectile if bullet drop is that important.

    Also if this isn't for a game with mega long range firefights (talking 1000m+) or a sniper game then there is absolutely no point in even considering bullet drops as it has no noticeable effect in the ranges most FPS games operate in.
     
    dudester likes this.
  5. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    makes sense , ill try that out thanks.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    "add a downward vector to your ray generation"
     
  7. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    Guys, don't make this any more complicated than it needs to be! Make your bullet an actual object with and attach a rigidbody to your bullet, and just let it do the gravity physics. Make your collider a trigger. Spawn it and add force to it, instead of raycasting. Then every FixedUpdate() you linecast from where the bullet was in the previous FixedUpdate to where i currently is!
     
    Last edited: Jan 28, 2016
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Yikes - I can't imagine the overhead of Physics.Simulate() if you did that. :)

    You can get a parabolic arc if you also factor in air resistance so the bullet slows down as it travels.
     
  9. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    "add a downward vector to your ray generation" , where do you say anything like that in your answer ?
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Sometimes devs give other devs more credit than deserved when it comes to understanding stuff. His pic made sense to me. Just because you didnt get it, doesnt make it unhelpful.

    combining raycasting and bullet drop make no sense. One is a hitscan method. I would like to know the motivation for trying to use what I consider conflicting methods of hit detection.

    Only application of using a raycast in this situation would be to make sure your ai isnt trying to shoot a wall.
     
  11. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Fair enough , helps to explain a bit more for the less experienced devs , also in hindsight our now understand what he meant
     
  12. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    ive done it heaps. im terribad for doing it.

    need a level indicator next to posters names so you can tailor the answer. I definitely answer differently based on the readers level.
     
  13. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Great idea a sort level which you can set yourself , for now i think using your signature will help but unity should definitly look into it.
     
  14. DirtyHippy

    DirtyHippy

    Joined:
    Jul 17, 2012
    Posts:
    224
    Iterative raycasts over the physics timestep taking into account whatever variables you want (bullet drop, wind, etc). This becomes even more important once you start dealing with lag compensation and hitbox rewinding. You use rigid bodies once you start dealing with non-determinism - i.e. bouncing projectiles, homing projectiles to a degree, etc mainly because you can't predict them accurately on clients so they need to be synced.

    I've heard Battlefield 4 uses raycasts for larger, predictable projectiles like cannon fire as well. This one I am not sure how they really handle it. The issue here is that we are dealing with a larger projectile than a bullet, and either A) you would just say screw it and only use a single raycast because the cannon shell is so fast no one would ever notice or B) fire multiple raycast "samples" that form the relative collision volume you need and iterate them all, which cascades to making lag compensated queries more complex as well.