Unity3d Space Conversion Learning Note (iii): 3D Math

Source: Internet
Author: User
Tags modulus

3D Math Base Vector

A vector can be seen as a line segment with direction and size.

For example: If we subtract point B from point A, we can get a vector that has a direction of point B facing point A, and a distance of two points. This method is often used in game development, for example, we want to object B to object A, usually using the position of object B minus the position of object A, the resulting vector modulus.

Dot Product

The dot product of a vector represents the projection of a vector on another vector, as follows:

The two vector dot product perpendicular to each other is 0:

We can use this feature to determine whether two vectors are perpendicular.

Unity provides the following method for calculating the dot product:

 Public Static float Dot (Vector3 LHS, Vector3 RHS);
Cross product

The cross product of a vector can get a 3rd vector perpendicular to two vectors, as follows:

The direction of the resulting vectors is determined by whether we are using a left-handed or right-handed coordinate system.

Unity provides the following method for calculating the cross product:

 Public Static Vector3 Cross (Vector3 LHS, Vector3 RHS);
Application of 3D Mathematics in Unity3d Vector3 modulus
    1. Vector.normalized: Returns the modulus of the current vector, the value of the vector itself is unchanged;
    2. static method Vector.normalize (VECTOR3): Returns the modulus of the parameter vector, the value of the parameter itself is unchanged;
    3. Vector.normalize: No return value, the vector itself programming its own modulo;
Demo06

Here is our example of making a North arrow, we need to make a small planet and a little man walking on this small planet, and then in the production of a North arrow, which is always pointing to the North Pole of the small planet.

Small Planet Production

Production of small planets I used a plugin: maoots (click here to download), below we step-by-step to achieve the small planet and the little man wandering on the planet.

1. First import the Maoots plugin;

2. Add a linear light source to the scene;

3. Add an empty object man, and then find the MAGICBOOTS\FBX\PENELOPEFBX prefab and add it as a child of the empty object ( Note that you must use the model provided by the plug-in, because the plugin's code has the Model animation control code, If you run with your own model, it will not work properly due to an error in animation .

4. Add Capsulecollider and Rigidbody to the man and adjust the position of a collider ( parameters please set and consistent ), as follows:

5. Add the Magicboots\scripts\magiccamera to the main camera and set the PENELOPEFBX under Man to target Character, and set the parameters as follows:

6. Add a layer named "Level";

7. Add a sphere, this sphere is our small planet, set its layer to "level" and add a collision body (from the line), adjust the position of man and Small Planet, as follows:

8. Add the Magicboots\scripts\magicboots script to the man and make the following settings:

9. OK, now it's time to run and see, no accident, our character can run on the small Planet;

10. Add a small flagpole to the top of the small planet to represent the North Pole, while adding a small arrow to the head to indicate the North needle, our compass will always point to the small flagpole, add the sky box and ground map to beautify the scene.

Note: Since I am using the Unity5, there will be a tilt problem when moving the character continuously, the problem is not on the Unity4.

North Needle making

Mainly using the operation of the cross product, we add the following code to the camera:

1 usingUnityengine;2 usingSystem.Collections;3 4  Public classDemo06script:monobehaviour5 {6     /// <summary>7     ///villain.8     /// </summary>9      PublicTransform player;Ten  One     /// <summary> A     ///Planet. -     /// </summary> -      PublicTransform Planet; the  -     /// <summary> -     ///rotation speed per second. -     /// </summary> +      Public floatRotatespeed = $; -  +     //the corresponding upper direction A     PrivateVector3 Playerup; at     PrivateVector3 Planetup; -  -     //North Point Needle -     PrivateTransform Compass; -  -     voidAwake () in     { -Playerup =player.up; toPlanetup =planet.up; +  -Compass = Gameobject.find ("Man/compass"). Transform; the     } *      $     voidUpdate ()Panax Notoginseng     { -Playerup =player.up; thePlanetup =planet.up; +  A         //find the cross product of the vector on the direction of the planet and the villain, and get a plane theVector3 Crossvalue =Vector3.cross (Planetup, Playerup). normalized; +  -         //The cross product of the cross product obtained by the villain and the upper part, this line points to the Arctic $Vector3 Northvalue =Vector3.cross (Playerup, crossvalue). normalized; $  -         //Draw Segment -Debug.drawline (player.position, player.position + player.up * -, color.black); theDebug.drawline (player.position, player.position + planet.up * -, Color.green); -Debug.drawline (player.position, player.position + crossvalue * -, Color.Blue);WuyiDebug.drawline (player.position, player.position + northvalue * -, color.red); the  -         //North Arrow with swivel fingers Wuquaternion target =quaternion.lookrotation (Northvalue, Playerup); -         //rotate a certain angle per frame AboutCompass.transform.rotation = Quaternion.rotatetowards (compass.transform.rotation, Target, Rotatespeed *time.deltatime); $     } -}

Run to see the effect.

Project file Download

Http://pan.baidu.com/s/1sjQJ5j3

Unity3d Space Conversion Learning Note (iii): 3D Math

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.