[Unity3D] Unity3D game development: 3D item display by mouse rotation and scaling; unity3d Game Development

Source: Internet
Author: User
Tags float max

[Unity3D] Unity3D game development: 3D item display by mouse rotation and scaling; unity3d Game Development

Dear friends, I am Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei. Recently, bloggers have focused on camera rotation, scaling, and other issues. Today we will share with you a feature that is commonly used in 3D presentations, that is, right-click the mouse to achieve rotation, scroll wheel to achieve scaling, and middle mouse to achieve translation. The method used in this article is still the method used in the [Unity3D] Unity3D game development from the free perspective of role control in this article, so if you do not know much about this part of content, you 'd better read this article first. Now, we will use a specific example to explain today's content ., It is a simple scenario created by a blogger. We hope to achieve the rotation of the role by right-clicking the role, scaling the role by the scroll wheel, and moving the role by the middle mouse.


Now let's create a script named FreeView, which is attached to the master camera. Let's take a look at its implementation process:

Using UnityEngine; using System. collections; public class FreeView: MonoBehaviour {// observe the Target public Transform Target; // observe the Distance from public float Distance = 5F; // the rotation speed is private float SpeedX = 240; private float SpeedY = 120; // angle restriction private float MinLimitY =-180; private float MaxLimitY = 180; // Rotation Angle private float mX = 0.0F; private float mY = 0.0F; // private float MaxDistance = 10; private float MinDistance = 1.5F; // mouse zoom speed p Rivate float ZoomSpeed = 2F; // whether to enable difference public bool isNeedDamping = true; // speed public float Damping = 10F; // The storage angle of the Quaternary private Quaternion mRotation; // define the mouse button enumeration private enum MouseButton {// left mouse button MouseButton_Left = 0, // right mouse button MouseButton_Right = 1, // middle mouse buttons MouseButton_Midle = 2} // camera movement speed private float MoveSpeed = 5.0F; // screen coordinate private Vector3 mScreenPoint; // coordinate offset private Vector3 mOffset; void Start () {// initialize the Rotation Angle mX = transform. eulerAngles. x; mY = Transform. eulerAngles. y;} void LateUpdate () {// right-click to rotate if (Target! = Null & Input. getMouseButton (int) MouseButton. mouseButton_Right) {// get the mouse Input mX + = Input. getAxis ("Mouse X") * SpeedX * 0.02F; mY-= Input. getAxis ("Mouse Y") * SpeedY * 0.02F; // The range is limited to mY = ClampAngle (mY, MinLimitY, MaxLimitY); // calculates the rotation of mRotation = Quaternion. euler (mY, mX, 0); // use different angle calculation methods based on whether interpolation is performed. if (isNeedDamping) {transform. rotation = Quaternion. lerp (transform. rotation, mRotation, Time. deltaTime * Damping);} else {transform. rotation = mRotation; }}// move the middle mouse button // zoom the Distance-= Input. getAxis ("Mouse ScrollWheel") * ZoomSpeed; Distance = Mathf. clamp (Distance, MinDistance, MaxDistance); // recalculate the position Vector3 mPosition = mRotation * new Vector3 (0.0F, 0.0F,-Distance) + Target. position; // set the camera position if (isNeedDamping) {transform. position = Vector3.Lerp (transform. position, mPosition, Time. deltaTime * Damping);} else {transform. position = mPosition ;}/// angle restriction private float ClampAngle (float angle, float min, float max) {if (angle <-360) angle + = 360; if (angle> 360) angle-= 360; return Mathf. clamp (angle, min, max );}}
Do you think this part of code is very familiar? Yes, the principles here are the same. You only need to master the Core Principles, that is, the rotation angle is calculated based on the mouse input, the distance is calculated using the scroll wheel input, and the angle and distance of the camera are calculated using the Quaternary element calculation. Okay. Let's take a look at the final effect!



It should be noted that the blogger has not implemented the relevant method of translating by pressing the middle mouse, because this part of the blogger has not been completely written correctly yet. If you know, you can leave a message to the blogger, or wait for the blogger to figure out when to update the blog. Blog posts of bloggers will be updated frequently. I hope you will continue to pay attention to my blog. Now, let's make another migration. We will port this function to the Android platform. I wanted to write this article separately as Android gesture control, however, I feel that the code here is similar and it is not necessary to write another article. Therefore, let's give the code directly!

Using UnityEngine; using System. collections; public class MobileInput: MonoBehaviour {// observe the Target public Transform Target; // observe the Distance from public float Distance = 2F; // The maximum value from public float MaxDistance = 5F; public float MinDistance = 0.5F; // scaling speed private float ZoomSpeed = 0.15F; // rotation speed private float SpeedX = 240; private float SpeedY = 120; // The angle limit is private float MinLimitY = 5; private float MaxLimitY = 180; // The rotation angle is private float mX = 0; private Float mY = 0; // The current gesture private Vector2 mPos; void Start () {// allow multi-touch Input. multiTouchEnabled = true; // initialize the rotation mX = Target. eulerAngles. x; mY = Target. eulerAngles. y;} void Update () {if (! Target) return; // single-touch if (Input. touchCount = 1) {// if (Input. touches [0]. phase = TouchPhase. moved) {mX + = Input. getAxis ("Mouse X") * SpeedX * 0.02F; mY-= Input. getAxis ("Mouse X") * SpeedY * 0.02F; mY = ClampAngle (mY, MinLimitY, MaxLimitY) ;}// multi-touch if (Input. touchCount> 1) {// both fingers are in the moving state if (Input. touches [0]. phase = TouchPhase. moved | Input. touches [1]. phase = TouchPhase. moved) {// calculate the direction of the mobile Vector2 mDir = Input. touches [1]. position-Input.touches [0]. position; // determine whether the current gesture is enlarged or reduced if (mDir. sqrmagn.pdf> mPos. sqrmagnstance) {Distance-= ZoomSpeed;} else {Distance + = ZoomSpeed;} // Distance restriction Distance = Mathf. clamp (Distance, MinDistance, MaxDistance); // update the current gesture mPos = mDir ;}// calculate the camera angle and position transform. rotation = Quaternion. euler (new Vector3 (mY, mX, 0); transform. position = transform. rotation * new Vector3 (0, 0,-Distance) + Target. position;} // angle restriction private float ClampAngle (float angle, float min, float max) {if (angle <-360) angle ++ = 360; if (angle> 360) angle-= 360; return Mathf. clamp (angle, min, max );}}


The following figure shows how the program runs on a mobile phone.









Today's content is like this. The blogger has recently been playing tricks on the integration of Unity and Android. I hope you will like it when the blogger studies it well. Finally sent:


Daily proverbs: Life is a kind of rhythm. There must be light and shadows, and there must be left and right. Life is an endless wandering. In the face of unexpected accidents in life, sudden injuries, and disasters falling from the sky, I believe that as long as there is love, enough courage to withstand all the storms in life, everything can be relaxed and relieved.




If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/39253951




How to Use UNITY3D to display an item model (rotating, scaling, and translating)

Script Writing implementation
 
How to rotate scenes in unity3d

Use camera as a cube sub-object. The orientation of the two is the same as that of the Z axis. In the script that gives the mouse a visual field to camera, see the script in FirstCharacterController in the official CharacterController package.
 

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.