Unity3d using the Euler angle to achieve the rotation of solids

Source: Internet
Author: User
Tags in degrees

Tag: use RAC Blank local its NIM not right-click visible

Just getting started u3d, but also a lot of things do not understand, the first contact is to try to get the keyboard of getpress and other events it

The official API Doc is also a lot of help, absorbing all the bloggers of the article also benefited from ~

Then again, the recent problem is how to create a new camera and use the mouse to look around the scene, try to find transform. Rotate not be able to implement this event well

(ps:z Axis will Move)

Later found to have to use four yuan or Euler angle to solve, these two are transform inside a few tigers

Put a video of what you saw, about Euler's corner.

In fact, the angle of adjustment in the property view of the game object is the Euler angle, and the rotation of the object when the scene is edited is represented by the Euler angle.

Just like this (picture quoted from ☆a Billion)

The official documentation has an explanation:

--------------------------------------------------------------------------------

Transform.eulerangles Euler angle

var eulerangles : Vector3

Description Description

The rotation as Euler angles in Degrees.

Rotate to Euler angle

The x, y, and z angles represent a rotation z degrees around the z axis, x degrees around the x axis, and y degrees around The Y axis (in this order).

x, y, z angle for z-axis rotation around the z-axis, x-degree rotation around the x-axis, y-degree rotation around Y

Variable to read and set the angles to absolute values. Don ' t increment them, as it would fail when the angle exceeds Degrees. Use Transform.rotate Instead.

Use this variable only to read and set angles to absolute values, and do not increment them. When the angle exceeds 360 degrees it will Fail.

Use Transform.rotate Overrides.

--------------------------------------------------------------------------------

PS: Machine turned ..../funny

This is very abstract, but the official documentation also gives an example code

1 usingunityengine;2 usingsystem.collections;3 4  public classExample:monobehaviour {5      public floatYrotation =5.0F;6     voidUpdate () {7Yrotation + = Input.getaxis ("Horizontal");8Transform.eulerangles =NewVector3 (Ten, yrotation,0);9     }Ten      public voidAwake () { one Print (transform.eulerangles.x); a Print (transform.eulerangles.y); - Print (transform.eulerangles.z); -     } the}
View Code

And the precautions to use are marked BELOW.

Do not set one of the eulerangles axis separately (eg. eulerangles.x = ten;) Since this would leads to drift and undesired R Otations. When the setting them to a new value is set them all at once as shown Above. Unity would convert the angles to and from the rotation stored in transform.rotation

do not set one of the Euler angles separately (for example: eulerangles.x = ten;) because this will result in offsets and unwanted rotations.

When setting a new value for them, assign the entire value at the same time (new Vector3)

Because if assigned individually, gimbal lock may appear, that is, the universal Joint lock (see Video Above)

In other words, to rotate an entity with Euler's angle, you need to follow these steps:

1. Defining an increment variable for the XYZ axis

2. Get the x\y axis offset of the mouse with Input.getaxis

Note: as the code below says, we want to get the x-axis offset of the mouse to rotate the yaw of the camera, which is the y-axis, relative to the right-hand coordinate system, and get the y-axis offset of the mouse to move the Camera's pitch, which is the x axis;

And for the mouse to move up (y-axis offset +), The x-axis of the entity that needs to be rotated should be rotated negatively, so the x-axis of the new Vector3 should be written to the negative mouse y-axis offset

3. The transform.localeulerangles of the object is assigned a new Vector3, try to avoid the single-value change caused by the universal joint lock

4. additional, You can limit the angle of rotation of the XYZ axis to further reduce the probability of the joint lock, and let the movement of the view closer to the real (for example, when we look at things in the head, do not turn around to see only the positive or negative 90 degrees of things, or we raise the angle of not more than 90 degrees.

/funny unless you tilt your body upside down and look at Things. 23333

Put a code on it.

Note that this code, as summary at the beginning, detects that the parent that called it

Which means you add it to the camera, move the mouse, and the camera Rotates.

But you just add it to the object, only the object follows the mouse, and the camera (your Perspective) doesn't change.

usingunityengine;usingsystem.collections;/// <summary>///This script detects the parent that calls this component///and apply actions to the parent/// </summary>[addcomponentmenu ("Camera-control/mouse look")] public classmouselook:monobehaviour{/// <summary>    ///enumeration: axes that need to be rotated/// </summary>     public enumrotationaxes {mousexandy=0, MouseX=1, Mousey=2     }    /// <summary>    ///enumeration: mouse button values/// </summary>    Private enumMousebutton {mousebutton_left=0, Mousebutton_right=1, Mousebutton_middle=2    }    /// <summary>    ///Defining axis Variables/// </summary>     publicRotationaxes axes =rotationaxes.mousexandy; //mouse x, Y-axis sensitivity     public floatSensitivityx =3F;  public floatSensitivityy =3F; Private floatdamping =1.5f; //x axis maximum rotation angle     public floatMinimumx =-80F;  public floatMAXIMUMX =80F; //y axis maximum rotation angle     public floatMinimumy =-60F;  public floatMaximumy =60F; //z-axis deflection maximum rotation angle     public floatMinimumz =-15F;  public floatMaximunz =15F; //xyz Axis Increment    floatDeltamouserotationx =0F; floatDeltamouserotationy =0F; floatDeltarotationz =0F; //the XY axis position of the mouse at the previous update    PrivateVector3 lasttimeposition; voidUpdate () {if(axes = =Rotationaxes.mousexandy) {//gets the x-move increment of the mouse//want the camera to move yaw that Y axisDeltamouserotationx + = Input.getaxis ("Mouse X") * Sensitivityx *damping; //Compare x rotation angle to maximum, minimum rotation angle limitDeltamouserotationx =mathf.clamp (deltamouserotationx, minimumx, maximumx); //gets the y-move increment of the mouse//I want the camera to move pitch that X axisDeltamouserotationy + = Input.getaxis ("Mouse Y") * Sensitivityy *damping; //Compare the y rotation angle to the maximum and minimum rotation angle limitDeltamouserotationy =mathf.clamp (deltamouserotationy, minimumy, maximumy); //Lock XY Axis while holding right button, move Z axis only            if(input.getmousebutton (int) (mousebutton.mousebutton_right)) {//gets the x-move increment of the mouse//want the camera to move the roll, the z axisDeltarotationz + = Input.getaxis ("Mouse X") * Sensitivityx *damping; Deltarotationz=mathf.clamp (deltarotationz, minimumz, maximunz); Transform.localeulerangles=NewVector3 (transform.localeulerangles.x, transform.localeulerangles.y,-deltarotationz); }            //normal move view without holding right button            Else            {                //Euler angles in transform need to be assigned at once//in case the Universal joint lock occursTransform.localeulerangles =NewVector3 (-deltamouserotationy, deltamouserotationx,0); Lasttimeposition=transform.localeulerangles; //reset Z-axis RotationDeltarotationz =0f; }        }        Else if(axes = =Rotationaxes.mousex) {transform. Rotate (0, Input.getaxis ("Mouse X") * sensitivityx*damping,0); }        Else{deltamouserotationy+ = Input.getaxis ("Mouse Y") * Sensitivityy *damping; Deltamouserotationy=mathf.clamp (deltamouserotationy, minimumy, maximumy); Transform.localeulerangles=NewVector3 (-deltamouserotationy, transform.localeulerangles.y,0); }    }    voidStart () {//Make the rigid body isn't change rotation        if(getcomponent<rigidbody>()) {getcomponent<Rigidbody> (). freezerotation =true; Debug.Log ("Rigid Body name:\n"+ getcomponent<rigidbody>(). name); } cursor.visible=false; }}

Unity3d using the Euler angle to achieve the rotation of solids

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.