I haven't made an update in a week or so and that's because I haven't really had the chance to do so. Besides, the goals I set for myself in my last post still haven't been met, again, I haven't had the chance to do anything with the project. I did, however, have the chance to look over some things and realize that what I was doing as far as the animal randomly moving to different points, was very ineffective and would run into some issues. For example, the animal would only move in a positive direction, meaning it would only move west(right) and north(up), never east(left) or south(down).
Why was this happening?
This was happening because of how I set my random point positioning. I always set it to start a 0 for both my X and Z positions. For the purposes of my previous demonstration, it was fine since the animal would always start out at that position and since it was the south west corner of my terrain, but what if I had set my animal's starting position somewhere else, say, in the middle of the terrain or in the north east corner? Since it would only move in a positive direction, it would potentially not move at all.
What did I do?
So I fixed this by resetting the random point to the animal's position each time the animal stopped moving, then when the idle timer hit zero, I set an negative value of the minimum position for the X and Z positions as long as the resulting value for each position is greater than zero. If not, then the X and Z positions would be zero.
What else could go wrong?
Halfway through writing about this fix I made another realization, the positive values for X and Z could potentially lead the animal off of the terrain. This isn't a big deal if I were to do a spherical world with endless terrain(which I intend to do) but for a terrain that ends, this could be a problem. So to fix this, the maximum X and Z positions will have a check to see if they are the same as the maximum terrain width and depth. Another issue that is going to come up is, what happens if the terrain drops below a certain height or rises above a certain height? Some animals can't climb, after all and avoid things like trenches, or cliffs. This leads me into my next issue.
Rugged terrain issue
Terrain isn't flat and animals can move over terrain, climb and walk down hills, etc. The current setup I have for my animal is assuming that the terrain is flat, which means, there are no hills or valleys. Once I begin sculpting the terrain, there will be random points that are on hills or in valleys. With the way I have it now, The point will actually set correctly, however, the animal will 'float' above a lower point in a sunken terrain and 'sink' under a higher point on a raised terrain.
How do I fix?
My instinct says that I need to sample the terrain's height on each frame while the animal is moving and set the animal's position and rotation equal to the sampled position and rotation. However, logic tells me that the great minds over at Unity have a way for me to do this without having to worry about brute forcing the animal into behaving the way it needs to behave when dealing with rugged terrain, and that's with the NavMeshAgent. So my current task and goal is to restructure my entire animal and make it behave as an agent on a nav mesh. Which means, the script that allows my animal to move around is going to change drastically in order to accomplish this.
The only issue I'm constantly running into is setting the random point. Currently, I'm working with a flat terrain, so I have no need to change the height(Y) for the point, but once I sculpt my terrain, that will change. Again, my instinct tells me that when I set my random point to start the animal moving again, I'll need to sample the terrain height and allow that to be my height(Y-value).
No...well, yes, it is, but.......anyway.
I'm going to restructure my animal so that it behaves as a navigation agent.
I'm going to allow my random point to change its height based on where it's at on the sculpted terrain.
Once I have the performance right for a single animal, I'm going to add additional animals, which means I'll have to start considering what behaviors will occur when the animals interact(collide).
At the very least, expect to see the movement behavior done right in my next post, as well as seeing it perform on multiple animals. I will also have a sculpted and designed terrain. Hopefully I'll be able to get that done by the end of the weekend, if not, it'll definitely be done by this time next week.