Unity3d Angrybots Learning enemies rotate and move around the protagonist

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.