Search Unity

Help with sea creature AI

Discussion in 'Scripting' started by Krisper, Oct 13, 2013.

  1. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    I have been working for days and days on a script for some sea animals, so far a whale and a shark, but I just can't seem to get it right.

    I have variables set that gives them a depth zone, ie, the minimum depth and maximum depth that they can go in the ocean. I also set the minimum distance to a wall that they can go.

    I am using rigid bodies on them, and applying a continual upwards force for buoyancy, and a forward force for their swimming.

    Then I set random times and distances where they will turn or change depth. And every couple of seconds I send out some rays to see how close to the terrain they are, if they get too close then I make them turn a random amount. It works pretty well for the most part, but every now and then one of them will get tangled in some terrain, or if they collide with something they go crazy.

    I am using iTweens.RotatoTo for the changes in depth and direction, using random values for the amount to turn so they swim around in random directions.

    I am using an oncomplete function to flag when a change has finished before applying another change, otherwise multiple changes will queue up. I think part of the problem is that sometimes the oncomplete function is not reached. I have heaps of data being logged so I can see what is going on, but I am thinking I may have gone about this all wrong.

    I need to make the code as simple as possible as I plan on having lots of animals running the code. Should I use physics Torque to make direction and depth changes instead of iTween.

    Has anyone got some better ideas on how I should go about this?
     
  2. Jack Thomas

    Jack Thomas

    Joined:
    Jun 26, 2013
    Posts:
    34
    What exactly do you need 'help' with? You haven't posted any reference to your work or even snippets of functionality. Your technique seems pretty standard and sound although like i said, hard to give any feedback from a blog report.
     
  3. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    I am wondering about my approach, how would other experienced people approach something like this. I have just been guessing my way through it.

    Do you think using forces to move the object, and iTween to rotate the object is an OK solution? Would there be a better way with all Physics?

    I am also unsure how to handle collisions with the Terrain, ideally this shouldn't happen, but it still does sometimes. And the object goes mad, bouncing all over the place.