People who have seen the angrybots example know that the enemy may be the aircraft, there may be a robot, here we assume he is the aircraft, OK, we first set the aircraft is rigidbody, then fixed y postion, and x,z rotation, write down the following code
usingUnityengine;usingSystem.Collections; Public classBuzzemove:monobehaviour {PrivateTransform player;//goal, target PrivateRigidbody Rigidbody; Public floatFlyingspeed =5.0f;//Flight Speed Public floatZigzagspeed =2.5f;//set a speed of about PrivateVector3 smoothedirection = Vector3.zero;//make the move smoother Private floatBacktrackintensity =0.5f; Public floatOriantationmultiplier =2.5f;//rotation Factor//Use this for initialization voidAwake () {player= Gameobject.findgameobjectwithtag ("Player"). Transform; Rigidbody= getcomponent<rigidbody>(); } //Update is called once per frame voidfixedupdate () {Vector3 direction= Player.position-transform.position; Direction. Normalize ();//Normalization of DirectionSmoothedirection = Vector3.lerp (smoothedirection,direction,time.deltatime*3.0f);//The difference makes the motion smoothVector3 zigzag = transform.right* (Mathf.pingpong (Time.time*zigzagspeed,2.0f) -1.0f) * ZIGZAGSPEED;//add offset to left and right floatOrientationspeed =1.0f; Vector3 deltavelocity= (Smoothedirection*flyingspeed + zigzag)-rigidbody.velocity; //if the Vector3.dot two value is greater than 0, then two objects are face-to if(Vector3.dot (direction, Transform.forward) >0.8f) {rigidbody. Addforce (deltavelocity, Forcemode.force); } Else //Conversely, two objects should be away from the{rigidbody. Addforce (-deltavelocity *backtrackintensity, Forcemode.force); Orientationspeed= Oriantationmultiplier;//It spins faster in the opposite direction. } floatRotationAngle =Anglearoundaxis (transform.forward,direction,vector3.up); Rigidbody.angularvelocity= Vector3.up * rotationangle* oriantationmultiplier;//rotation of a rigid body//transform. LookAt (player, vector3.up); } //The angle between Dira and dirb around axis Static floatAnglearoundaxis (Vector3 dira, Vector3 dirb, Vector3 axis) {//Project A and B onto the plane orthogonal target axisDira = Dira-vector3.project (dira, axis); DIRB= DIRB-vector3.project (DIRB, axis); //Find (positive) angle between A and B floatAngle =Vector3.angle (Dira, DIRB); //Return angle multiplied with 1 or-1 returnAngle * (Vector3.dot (axis, Vector3.cross (Dira, DIRB)) <0? -1:1); }}
Unity3d Angrybots Learning enemies rotate and move around the protagonist