Search Unity

Get distance, add force to hit target

Discussion in 'Scripting' started by kodagames, Jun 2, 2020.

  1. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Hello,

    I need help with figuring out how to shoot a sphere and hit a specific target every time no matter the distance?

    Currently I’m getting the distance from the sphere to the target and adding a force (impulse) in the z direction (for the distance amount. Which works great at a specific distance but if I move the target a little further away of course when I shoot the sphere it will overshoot the target.

    Any ideas on how to make it so I can shoot from any distance and hit the target every time?

    Greatly appreciate any help (it doesn’t necessarily need to be coded :))
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Sounds like you want to use basic kinematic equations to calculate the force and direction required to shoot the sphere. I would look up and study kinematic projectile equations.
     
    kodagames likes this.
  3. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Wow that’s some tough stuff but thank you! It seems that having just a force and distance is not enough somehow I’ll have to calculate time, velocity, speed and acceleration (-9.8).
    Math is definitely not my strong suit... mind blown lol but appreciate you pointing me in the right direction I’ll post back because I can definitely see some fails on figuring out this one.
     
  4. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    I also stumbled upon parabola?
     
  5. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Awesome!!!
     
    carlosbriosoa likes this.
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    That should do it for you. Study hard, it's not too bad once you get your head around it.
     
    kodagames likes this.
  7. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    its greatly appreciated thank you so very much!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Hey @kodagames I think it won't be too bad. There are some other tutorials on trajectories... what you want to do is not so much add a force (it CAN be done, but it's fiddly because you have to back out the fixed time step, weight of object, etc.), but rather just set the velocity based on your calculations.

    Remember also that there are actually an infinite number of launch angles at which you can fling a ball into a basket. You probably would want to choose an angle (45 is a good one to start with, unless you're too close!!) and fix that, then calculate how fast it has to be at launch... otherwise there's two unknowns and not enough information.

    For an arbitrary solution, like if you're standing one millimeter to the side of the basket, I think you'd have to have a heuristic that says "I will go at least X times as high as the basket," and set the vertical component of the launch to reach that, and then select the lateral velocity based on time of flight back down to basket height, and lateral distance.
     
    beatsbytex and kodagames like this.
  9. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Hi Kurt,
    Thanks for the response I’ll take that into consideration, so far I’m loving the kinematic equations the tutorial above does a good just of explaining things in part 1 and in part 2 he even calculates how much time it would take for a missile to catchup and destroy another object really cool stuff.

    you guys are smart that’s for sure! (Way above my pay grade lol).
     
  10. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    kodagames likes this.
  11. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548