Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question about AI aiming and rigidbody drag

Discussion in 'Physics' started by Steve-Tack, Feb 12, 2015.

  1. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    I’m making a 3D space game that has an emphasis on realtime combat and I’ve got rudimentary enemy AI working. One problem is that with fixed forward-firing projectile weapons, they are terrible shots. :)

    The issue seems to stem from the fact that I’m using AddForce and AddTorque along with drag and angular drag settings. Drag isn’t strictly realistic in space of course, but I justify it by thinking of it as “braking thrusters” or something. Actually, the player can optionally turn off drag, but it’s hard to play that way. The player ship controls well with some drag, so my initial thought process was to have the enemies use the same “Ship” class with the same movement code.

    Enemies will try to predict where the player will be in the near future based on their current heading, taking into account the player’s speed and the speed of the projectiles. What’s tricky is that when they rotate into position, there’s a constant lag because of leftover momentum. It actually looks kind of cool, since they can sometimes sort of slide sideways for a bit in a realistic manner (though realism isn’t that important to me). But it makes hitting the player with projectiles really tough. Now I don’t want the AI to necessarily be TOO good, so it could be a way to adjust enemy accuracy. It seems that the higher the drag, the more accurate they are, but then I have to increase the forces to compensate.

    So I’m wondering if I should treat enemy ships differently than the player and just set their drag settings to infinite and move and rotate them around directly. It kind of feels like giving up, but under the circumstances, maybe it makes sense.

    Any suggestions on a good approach to this?
     
  2. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,005
    My approach with enemy AI is always to make them deadly (perfectly) accurate and then add in things like hesitation and ranges of randomness, it makes it easier to steer your gameplay rather than just hoping something will end up feeling ok.

    To answer your question, if you want to have your cake like that, there's no way around it, you're going to have to solve you control equations with the inclusion of all of the forces acting on the ship. Your AI will do exactly what you program them to do so if you give them the wrong equations you can hardly expect them to correct them for you! If you don't have a head for physics and maths it might not be the best approach but otherwise I suggest getting a decent high school or first-year university Dynamics textbook as a start.

    Your problem won't be an easy one to solve but if you do find a good solution you can be pretty sure that others will want it and thats an opportunity :)
     
  3. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    Thanks for the reply!

    I've been experimenting with a small stand-alone project and and this point it's looking like a hybrid approach may work. I'll still use AddRelativeForce for forward (and reverse) NPC ship movement, but instead I may cheat a little and use Rigidbody.MoveRotation to move directly toward the point I want.

    I had been doing the full deal where the AI only has access to the controls the player has (pitch, roll, yaw, plus forward and reverse thrust). If I ease up on that and just rotate directly, I think that'll help a bunch. Increasing the normal drag helps minimize the sideways motion, though too much makes the ships stop too quickly when they cut engines.

    I'm also considering giving NPC ships turrets rather than fixed weapon barrels to help compensate for any drifting. Of course that has its own set of math to work out, but at least that's a problem that's been solved a bunch of times. So I imagine I can Google that one.

    Yes, the goal would be to have perfectly accurate NPC ships that I can then dial back as needed. I'm hopeful I can get close to that without having to read any textbooks or write my own physics engine. :)