- Home /
Question about navMeshAgent speed and the "Time" variable
I get a weird outcome when i try to program my navMeshAgent's speed. I want it to arrive at the target at a specific time, but somehow it manages to arrive earlier. (Although that's not so bad, since I'm modelling bus routes, it's nice for the bus to arrive early!)
To be more specific, lets say I have 2 gameObjects, target1 and target2. When the agent reaches target1, it gets a new destination, target2. target1 has a variable in a script called target1atts.js that's attached to it, containing only the line:
var arrTime = 10.0 // arrival time for target 1
Likewise, target2 has the target2atts.js script attached, containing:
var arrTime = 30.0 // arrival time for target 2
Furthermore, using the NavMeshAgent.CalculatePath function, I know the path length between the 2 targets is 420. (Quick question here, what units is this length in?) In a script attached to the navMeshAgent, I have the following:
var depTime = 10.0 //the time the agent arrived at target1
var arrTime = 30.0 //the time the agent needs to arrive at target2
var tripDuration = arrTime - depTime
var pathLength = 420 //taken from NavMeshAgent.CalculatePath
var spd = pathLength/tripDuration //effectively the speed for this trip, equals 21
agent.speed = spd
Given that the navMeshAgent is at target1 when Time.time equals 10 (give or take some ms), it should follow that with the above calculations, it would reach target2 when Time.time equals 30 (give or take the same amount of ms). However, it consistently arrives at target2 at around 23 seconds. I would expect it to arrive later than the predetermined time, due to deceleration during turns, but in no way it should arrive earlier, or even that earlier.
I'm stumped, any clues as to what's wrong? I'm almost ready to hold a maths book against the screen, screaming "The power of math compels you!"
Thank you for any insight.
"Quick question here, what units is this length in?"
You should really always, always, always use meters and $$anonymous$$G in game development. Humanoids must be actually 1.8m high, cars should be actually 3.2n long and weigh 1700kg, and so on.
If your modelmakers send something over "hey just change the scale by 17.42 dude" email right back "hey just build it properly and we'll hey pay you" :-) (Also of course, every single item must be z-forward.)
So anyways meters, yeah
just BTW 21 m/s is totally wrong for humanoid locomotion, so something's screwed.
(You can always easily remember the top running speed for humans, by just thinking of Usain Bolt's performance .. so, only a handful of humans can reach 10 m/s.)
{Naturally if you're a video game programmer, you don't have to 'remember" that, it would be like remembering how many fingers you have, but I supply that information for new readers! Heh!! :) }
Hey Fattie, thanks for the quick answer!
It's good to know that it's up to me to define the correct units in Unity. I'm using an urban scene imported from cityEngine, and with the default First Person Controllers that I've tested it with everything is in the correct scale, but I'll have another look at it, just in case.
As to your second comment, the agent is actually a bus, and the targets are bus stops. Sooooo, 21 m/s would not be THAT crazy. (O$$anonymous$$, only if my bus was starring in "SPEED", but still 75 kph is not extraordinary). Furthermore, both the locations and the arrival times were put at random, as this is more of a proof-of-concept project. For other trips for example, the calculated speed goes up to 30 m/s. So, I'm not judging any results on the actual numbers yet, just the concept.
Oh silly me...
I was calculating the total path length from one target to another using this code:
function calculatePathLength(){
var path: Nav$$anonymous$$eshPath = Nav$$anonymous$$eshPath();
agent.CalculatePath(targ.transform.position, path);
Debug.Log("next path segments: " + path.corners.length);
var pathElements = path.corners.length;
for(i=1;i<pathElements;++i)
{
pathLength += $$anonymous$$athf.Abs(Vector3.Distance(path.corners[i-1], path.corners[i]));
}
}
The problem is, I never actually cleared the pathLength variable between different targets, in essence looking at the total distance travelled since time 0. Simpy adding
pathLength = 0.0;
at the start of the function made it work like a charm.
Silly, silly me..
Answer by Fattie · May 16, 2013 at 08:55 PM
OK first try your experiment with a straight path .. no turns. See what happens.
Sounds reasonable enough.. Unfortunately the project is at the work computer, I'll try it tomorrow and update accordingly. Thanks