Basic AI script. need help.

ok, so im working on a basic 2d AI script. The enemies are to simple home in on the player. however they dont go straight there, but have a steering limit… basically they have to actually turn around instead of doing an instant 180. however i keep getting really weird behavior and would appreciate any help.

void Update () {
//find direction to player

``````    direction = Vector3.Normalize(player.transform.position - this.transform.position);
float rot_z = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
target.transform.Rotate(0, 0, rot_z - 90f);

//determine left or right
if (Mathf.Abs(target.transform.rotation.eulerAngles.z - transform.rotation.eulerAngles.z) < 179) {
left = true;
}
else { left = false; }

/*if (Mathf.Abs(target.transform.rotation.eulerAngles.z - transform.rotation.eulerAngles.z) < turnSpeed) {
turnSpeed = 0;
}*/
if (!left) {

turnSpeed += turnAccel;
}
else if (left) {
turnSpeed -= turnAccel;
}

if (turnSpeed > turnAccelMax) { turnSpeed = turnAccelMax; }
if (turnSpeed < -turnAccelMax) { turnSpeed = -turnAccelMax; }
speed += speedAccel;
speed -= Mathf.Abs(turnSpeed);
if (speed <= 0) { speed = 0; }

if (speed > speedMax) { speed = speedMax; }

//target.transform.position = transform.position;
//target.transform.rotation = transform.rotation;

//target.transform.Rotate(0, 0, -turnSpeed);
target.transform.RotateAround(transform.position, Vector3.forward, turnSpeed);
target.transform.Translate(new Vector3(direction.x * speed * drag, direction.y * speed * drag, 0), Space.Self);

// currently target does not rotate correctly
rigidbody.MovePosition(new Vector3(target.transform.position.x, target.transform.position.y, 0));

transform.Rotate(0,0,-turnSpeed);
``````

}

That is a lot of code to sort through, but one thing I would like to point out immediately is a couple huge things. Number one, physics (like moving around, rotating, etc.) should be done in the FixedUpdate() method, NOT the Update() method. That could be causing your problem right off the bat. FixedUpdate is called every physics step and is what you should use when you are dealing with the physics of the game.

Number two, all your movement speeds need to be multiplied by the delta time, like this for example:
turnSpeed += turnAccel * time.deltaTime;

Notice the code I added there? This will now tell unity “I want to move my object by turnAccel units per second”. The * time.deltaTime is the “per second” in pseudocode. Everything that is dealing with updating the position/rotation of anything always needs to be multiplied by time.deltaTime. Not doing so could cause problems like the one you are experiencing now as well as framerate and other physics issues.

Number two, it’s possible the speed at which your A.I. is turning is set way too high. You might want to try super small units first like 0.01f and work your way up. And when programming A.I., you should code and test one thing at a time. For example, if your enemy is on the left of the screen and you want him to rotate towards the right, then you should first write JUST the code to do that, test, it, then make sure it works before you do the code for the right side. I hope this steers you in the right direcitons.