- Home /
Can you help me make this code turn smoothly?
I have two states in my Follower script - random wander, and following the player. When it wanders at random, it jerks at sharp angles to make turns. However, following the player it moves smoothly from point to point. I want to reconcile the two effects and have the wandering rotate smoothly as well.
direction.y = 0;
int speed = walkSpeed;
if (walkInRandomDirection) {
speed = randomSpeed;
}
if (executeBufferState) {
speed = runSpeed;
}
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
direction = forward * speed * speedModifier;
characterController.Move(direction * Time.deltaTime);
Please post the code you use to generate the directions for both random walking and when following the player. I don't think the problem is in this code.
Sure, although I haven't written in the following code yet. That came from a previous iteration that did not have random walking.
This is based off Ezzerland's simple AI behavior script, the full version of which is here: link text
void WalkNewPath() {
if (!walkInRandomDirection) {
walkInRandomDirection = true;
if (!playerHasBeenSeen) {
randomDirection = new Vector3(Random.Range(-0.15f,0.15f),0,Random.Range(-0.15f,0.15f));
} else {
randomDirection = new Vector3(Random.Range(-0.5f,0.5f),0,Random.Range(-0.5f,0.5f));
}
randomDirectionTimer = Time.time;
} else if (walkInRandomDirection) {
$$anonymous$$oveTowards (randomDirection);
}
if ((Time.time - randomDirectionTimer) > 4) {
//choose a new random direction after 2 seconds
walkInRandomDirection = false;
}
}
Could you describe the Jering a bit more? It seems that randomising the speed might be a problem...
I could probably do without that part, really. Ezzerland developed it to be an enemy AI script, I'm converting parts like this for a different type. - Okay.... did some checking and that's not the cause, all it does is ease forward.
There are some things here that are a bit strange to me and that I don't fully understand. I don't have time to flesh it out and put the scripts into Unity, so I'm going to make a couple of observations that may help.
It looks like in this line...
randomDirection = new Vector3(Random.Range(-0.5f,0.5f),0,Random.Range(-0.5f,0.5f));
...that the new random direction could be any direction. $$anonymous$$aybe what you want is more turns but only allow a limited change in direction from the current forward direction.
And in this line...
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
...this use of Slerp() will cause faster rotations for larger angles. You might experiment with Quaternion.RotateTowards() ins$$anonymous$$d.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092929im_/https://answers.unity.com/themes/thub/images/avi.jpg)