Exploration and analysis of automatic pathfinding and follow-up in Unity 3D

Source: Internet
Author: User

Here are the main three functions, an auto-follow function and two point rotation functions, these three functions are often used during the creation of a game character:

This is the follow function and the Euler angle rotation function.

public class Gensuizhixiang:monobehaviour {//Common Properties//define Transform Transform M_transform following the subject;    Defines the Transform public Transform target that is followed (pointed) to the object;    Follow the class//define the moving speed of the following body float m_speed = 10f;               Point to class//define the steering angle of the following body float m_rotspeed = 120f; Use the This for Initializationvoid Start () {//get itself transform M_transform = this.transform;}        Update is called once per framevoid Update () {//call following function follow ();    Call to function LookAt ();}        Follow function void Follow () {//define the original position following the principal (not yet followed by the move) Vector3 oldposition = m_transform.position;        Define the position of the object to be followed Vector3 targetposition = target.position;        Get the vector on the xyz axis float x = mathf.movetowards (oldposition.x, targetposition.x, M_speed * time.deltatime);        Float y = mathf.movetowards (OLDPOSITION.Y, TARGETPOSITION.Y, M_speed * time.deltatime);     float z = mathf.movetowards (oldposition.z, Targetposition.z, M_speed * time.deltatime);   The obtained vectors are assigned to follow the subject implementation following m_transform.position = new Vector3 (x, y, z);                    }//Point function void LookAt () {//define the original angle to the body (not yet rotated) Vector3 Oldoeuler = M_transform.eulerangles; Pointing to the body coordinates the steering is pointed to the object m_transform.        LookAt (Target.transform);  Obtain an object-oriented Euler angle (only XY can be achieved pointing to) float Eulerx = Mathf.movetowardsangle (oldoeuler.x, m_transform.eulerangles.x, M_rotspeed        * time.deltatime);        float eulery = Mathf.movetowardsangle (Oldoeuler.y, M_transform.eulerangles.y, M_rotspeed * time.deltatime);    The resulting rotational amount is assigned to the main realization point m_transform.eulerangles = new Vector3 (Eulerx, eulery, 0); }}

This is a four-dollar rotation function.

public class Quaternionlookat:monobehaviour {    //define Transform Transform M_transform that are directed to the subject    ;    Defines the Transform public Transform target;//of the object to be directed to    , Initializationvoid Start ()     {        // Initialize to the principal transform        m_transform = this.transform;} Update is called once per framevoid Update ()     {        //Call rotation function        Quaterlook ();}    Four dollar rotation function    void Quaterlook ()    {        //define a three-dimensional variable to represent the vector from the body to the object        Vector3 pos = target.position-m_ transform.position;        Four Yuan Lookrotation () to get the four yuan of the steering        quaternion m_rotation = quaternion.lookrotation (POS);        The resulting rotational amount is assigned to the rotating body to achieve a point rotation  (note here. Localrotation instead of. Rotation because the former can achieve rotation the latter cannot)        m_transform.localrotation = m_ rotation;    }    }

The Euler angular rotation function and the four-tuple rotation function both can achieve real-time point rotation but the four-dollar effect is better and finer

Exploration and analysis of automatic pathfinding and follow-up in Unity 3D

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.