I’m currently trying to write a function called “kinematicWander”. The purpose of this function is to have my AI character move in the direction of its current orientation at max speed and to randomly change its orientation. In other words, the AI is just wandering around randomly. When I call it later, it runs on an timer, so the character is supposed to wander around until the time is up. I’m having some trouble getting it to work though. Here is my current code for the function. My strategy was to generate a random point for the character to rotate towards, and have it move in that direction. I’m new to Unity so any help is much appreciated. Thanks
var wayPoint : Vector3 = Random.insideUnitCircle;
function kinematicWander(pos : Vector3){
while(remainingSeconds != 0){
var targetRotation = Quaternion.LookRotation(wayPoint.position, Vector3.up); //find the waypoint
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime*2); //rotate to the waypoint
transform.position = Vector3.MoveTowards(transform.position, wayPoint.position, Time.deltaTime*speed); //move to the waypoint
isWalking = true;
animation.Play("walk1");
}
}
Right now I’m testing all my functions by calling them in Update, they are when an input string matches the function name. Here is my Update at the moment, still making changes. Right now its set up so the user can move the character to wherever on the map using the arrow keys, then begin whichever function by typing it in. I plan on adding GUI buttons later. The other functions work. When Kinematic Wander is called, the character does nothing
Hey what happens if you change the while(remainingSeconds != 0) to ‘If(remainingSeconds >= 0)’
Im not very fluent in javascript but im assuming your getting stuck in a inifinite loop as remainingSeconds isnt decreased and so it is never equal to 0?
In the scene, “target” represents a stationary cube that is used for the seek and flee functions, the character simply moves to or away from the cube. wayPoint is this:
var wayPoint : Vector3 = Random.insideUnitCircle;
its intended to make the character turn to a random point, hence generating random rotation, which would make the character wander randomly
The problem seems to be that you are changing the random target with every Update. Actually I am not sure as I do not see where that wayPoint is being called.
Anyway you need to assign a random time amount and make AI follow the target for that time and change to another random target.
Actually there are easier ways to do this too (I think I had explained this before) You do not even need a target. Just make the AI rotate to a random direction at random intervals and make it walk straight.