Search Unity

SImple IA

Discussion in 'Scripting' started by GhostFire, Apr 2, 2010.

  1. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    Hi,

    The scripts for my game are almost complete.

    What i need is only to do a simple AI

    the enemy aim to you and shoot and change move direction when it collide with a box collider

    there is a tutorial for do this?
     
  2. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    992
    You can do a simple finite state style AI with different stimuli changing the state i.e. colliding with an object calls a function avoid() which sets the AI state to avoid mode. Each time the AI updates it checks what state it's in and acts accordingly. You can do this with if statements but you're likely to have a lot of states so might be better with a switch. Something like:

    switch(state){
    case "idle":
    // move to a random location or stand still
    break;
    case "alert":
    // AI is aware of an enemy and moves toward it
    break;
    case "shoot":
    // Enemy is in line of sight and firing range, shoot
    break;
    case "avoid":
    // AI hit an object, move around it
    break;
    }

    You can also have multiple states like shooting and walking or morale etc.
     
  3. Drew.Madsen

    Drew.Madsen

    Joined:
    Jan 19, 2010
    Posts:
    30
    Hints:

    Use Character controllers on your Characters. They have a Move function to make moving in a direction easier. They are also good for using ramps(stairs) and general movement issues.

    For rotation I suggest the RotateTowards that can be found on the Script Reference for turning slowly.

    transform.lookat can be used for instant rotation or constant rotation towards an object.

    For examples on how to make a gun and a Firing script, See the FPS tutorial.
     
  4. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    I agree with Drew that the FPS tutorial is the best place to find simple but effective AI.
     
  5. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    ok,thanks :)
     
  6. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    mm now it work but...my tank is not correctly aligned



    where i wrong?
     
  7. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    Make sure you tank is lined up correctly in the modeler.
     
  8. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    for modeler what do you mean? the program who i have used for made the tank or a tool inside unity?

    if you mean the program i have used 3ds and all pivot are align correctly (i have test also with another models)
     
  9. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    ok i resolved this but now i have got a new problem,

    when i create a build the IA doesn't work... tank doesn't follow the IA and stay freeze...
     
  10. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    There can be many reasons for this. Search the forums.
     
  11. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    i do a search but i don't have found anything for fix this :(
     
  12. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    Ok, I did a search too and it seems to be a bug unless there is a problem in your code we don't know about. If it works in the editor, but not in the build it is almost always a bug though so file a report.