U3D Rocker Technology _U3D rocker Rod

Source: Internet
Author: User
Tags ranges
1. Download the joystick plugin (I uploaded one)
! [Write a picture description here] (https://img-blog.csdn.net/20180125231505826?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTNkXzIwMTcxMDMw/ Font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/southeast)
2. Create a Easytouch
Click "Creat" to create Easytouch
 ![ Here to write a picture description] (https://img-blog.csdn.net/20180125231611287?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvdtnkxziwmtcxmdmw/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast)
3. Create Joystick
Right-click the created Easytouch to create joystick
 ![ Here to write a picture description] (https://img-blog.csdn.net/20180125231943222?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvdtnkxziwmtcxmdmw/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast)    
4. Get Joystick
This is the time to play the programmer's function, start the script
    Public Etcjoystick joystick;//here to get the joystick object
5. Determine the direction according to the AXISX and Axisy values of joystick
Keep getting values in update ()
        float j_x = joystick.axisX.axisValue;
        float j_y = Joystick.axisY.axisValue;
6. With axis values, it can be used to do all sorts of things, for example, to control role movement
    Private Charactercontroller player;//role controller
    private float[] X_scale = new Float[8] {0, 0,-1, 1, 0.707f, -0.707f,-0.7 07f, 0.707f};
    Private float[] Z_scale = new Float[8] {1,-1, 0, 0, 0.707f, 0.707f, -0.707f, -0.707f};
    Private float[] rot = new Float[8] {0, 180, -90, N, -45, -135, 135};

    void Start () {
        player = transform. Getcomponent<charactercontroller> ();
    }
 the number X_scale and Y_scale created here are eight values, in two-dimensional coordinates if the point (x_scale[value],y_scale[]), such a point, in the brackets in the "value" is an int data, so there will be eight points. The eight dots are up and down.
, left, right, upper left, lower left, upper and lower right, these eight positions. This is the eight point of the rocker, when the drag-and-drop rocker is located at these eight points, the line segment formed by the connecting origin intersects with the x axis, and the angle is the eight angles in the rot array, that is, the direction of the protagonist, the orientation is determined, as long as the protagonist moves forward to realize the protagonist control movement effect must
The rocker itself is located at the origin, with the original point as the center, the radius of 1 draw a circle, when the drag to the circle of the most right time, you get a point (1,0), the top point is (0,1), the upper right corner of the point is about (0.707,0.707). We set these eight points, and when the rocker is close, we think the protagonist is moving in that direction.
So use the following methods to determine the direction. 
    void Update () {float j_x = joystick.axisX.axisValue;
        float j_y = Joystick.axisY.axisValue;
        if (j_x*j_x+j_y*j_y<=0.25)//Make sure the rocker moves enough to prevent misoperation {return; float R = mathf.atan2 (j_y, j_x);//Compute angle int dir=get_dir (r);//get direction float s = speed * Time.deltatim
        E
        float sx=x_scale[dir]*s;
        float sz=z_scale[dir]*s; Player.

        Move (new Vector3 (SX, 0, SZ));
        Vector3 rot = this.transform.localEulerAngles;
        Rot.y = This.rot[dir];
    This.transform.localEulerAngles = rot;
    ///<summary>///To determine the movement direction based on the tan value///</summary>///<param name= "R" >tan value </param> <returns> Move Direction </returns> int Get_dir (float r) {if r>7*mathf.pi/8| |r<=-7*mathf.pi /8) {return (int) DIR.
        Left; else if (R >-7 * mathf.pi/8 && R <=-5 * mathf.pi/8) {return (int) DIR. LD else if (R >-5 * mathf.pi/8 && R <=-3 * mathf.pi/8) {return (int) DIR.
        Down; else if (R >-3 * mathf.pi/8 && R <=-1 * mathf.pi/8) {return (int) DIR.
        RD; else if (R > 1 * MATHF.PI/8 && R <= mathf.pi/8) {return (int) DIR.
        right;
        else if (R > Mathf.pi/8 && r <= 3 * mathf.pi/8) {return (int) dir.ru; else if (R > 3 * MATHF.PI/8 && R <= 5 * mathf.pi/8) {return (int) DIR .
        up; else if (R > 5 * MATHF.PI/8 && R <= 7 * mathf.pi/8) {return (int) DIR.
        LU; } return (int) DIR.
    up;
    ///<summary>///Direction///</summary> enum DIR {no_muve =-1, up = 0, down = 1, left = 2,
 right = 3, RU = 4, LU = 5, LD = 6,   RD = 7} 
Tan () returns the radian corresponding to an angle, for example, when x and Y are equal to one, the radian value corresponding to tan (45) is returned--1. And this 1 is the direction of the upper right corner. The first "4" position of the corresponding array: x_scale[4] and y_scale[4]. The enumerated array is only for the convenience of remembering the array position of the direction.
here the idea of the code: we convert the angle into radians to facilitate the calculation, the 360 degree is 2π into eight ranges, when the rocker into different ranges think of the array X_scale and Y_scale composed of eight points in the direction of movement. The
rot array is the angle corresponding to these eight points, and if the drawing analysis is clearer. The conversion to radians is only for convenience, and we use the role controller to control the role orientation with Euler angles, so we have to use angles, where the rot array corresponds to the other two arrays. For example: If x and Y are equal to 1, the corresponding array X_scale and Y_scale subscript are the same as the 4,rot also for [4] is 45 degrees, you can adjust the lead Euler angle, and the protagonist is around the y axis rotation, so use this code to adjust the lead direction
        Vector3 rot = this.transform.localEulerAngles;
        Rot.y = This.rot[dir];
        This.transform.localEulerAngles = rot;

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.