Unity3d game engine IOS touch screen gesture control lens rotation and Scaling (8)

Source: Internet
Author: User
Unity3d game engine IOS touch screen gesture control lens rotation and Scaling



Original by Yu Song MomoArticleIf you reprint it, please note: Reprinted to my independent domain name blogYusong Momo program Research Institute , Original address: http://www.xuanyusong.com/archives/512



The previous articles introduced many unity3d engine problems. Today we will go back to iOS devices to discuss some touch screen gestures. This chapter aims to achieve models by touching iPhone screen gestures.Left and rightAnd the scaling of the model.


Let's think about the rotation of the model, which is actually the rotation of the lens. The scaling of the model is actually the coordinate of the zaxis of the lens. To implement this chapter, you only need to control the camera position.


In the game scenario, we create some simple models as reference objects and insert the following sentence: "Who knows. fbx models can be found for free. I want to add a game model in the blog to make the scenario look better, but it is a bit depressing if I cannot find it, I think it is a bit inappropriate to write a blog post using the company's model. "Wow, if you know something, tell me. La la.



We create a simple game plane and place a box in the plane as a reference object for rotation and scaling. As shown in, select the camera and add a script named move to the camera. the script has a parameter target, which is used to set the camera to rotate and move the reference object. Here, a box is assigned to the target, so sliding the screen left and right will find that the box is rotating, zoom in and zoom out the box.








Let's take a look at the move script to illustrate several important aspects:

These methods are called by the system.

Function start (): This function is called only once after the game is started and can be used for script initialization,

Function Update (): After the start () method is called, each frame is called. You can update the game logic here.

Function lateupdate (): After the start () method is called, each frame is called, but it is called after the update () method is called.


Move. js completeCode

// Bind the reference object var target: transform; // The scaling factor var distance = 10.0; // The left and right sliding speed var xspeed = 250.0; var yspeed = 120.0; // zoom limit coefficient var yminlimit =-20; var ymaxlimit = 80; // The camera position var x = 0.0; var y = 0.0; // record the last touch position of the mobile phone to determine whether the user is zoomed in or out on the left. Private var oldposition1: vector2; private var oldposition2: vector2; // initialize the Game Information and set function start () {var angles = transform. eulerangles; X = angles. y; y = angles. x; // make the rigid body not change rotation if (Rigidbody) Rigidbody. freezerotation = true;} function Update () {// determines the number of touch points as single-touch if (input. touchcount = 1) {// The touch type is mobile touch if (input. gettouch (0 ). phase = touchphase. moved) {// calculate the X and Y positions x + = input based on the touch point. getaxis ("Mouse X") * xspeed * 0.02; y-= input. getaxis ("Mouse y") * yspeed * 0.02;} // determines the number of touch points. If (input. touchcount> 1) {// The first two finger touch types are all moving touch if (input. gettouch (0 ). phase = touchphase. moved | input. gettouch (1 ). phase = touchphase. moved) {// calculate the position of the current two-point touch point var tempposition1 = input. gettouch (0 ). position; var tempposition2 = input. gettouch (1 ). position; // if the function returns a scaled-in value, the false value is returned. If (isenlarge (oldposition1, oldposition2, tempposition1, tempposition2 )) {// After the amplification factor is greater than 3, you cannot zoom in. // The data here is adjusted based on the model in my project. You can modify if (distance> 3) by yourself) {distance-= 0.5 ;}} else {// The data here is adjusted according to the model in my project, you can modify if (distance <18.5) {distance + = 0.5 ;}/// back up the last touch point location, which is used to compare oldposition1 = tempposition1; oldposition2 = tempposition2 ;}}// if the function returns a zoom-in value, the false value is the zoom-out function isenlarge (OP1: vector2, OP2: vector2, Np1: vector2, NP2: vector2 ): boolean {// The function transmits the position of the last two points of touch and the position of the two points of this touch to calculate the user's gesture var leng1 = mathf. SQRT (op1.x-op2.x) * (op1.x-op2.x) + (op1.y-op2.y) * (op1.y-op2.y); var leng2 = mathf. SQRT (np1.x-np2.x) * (np1.x-np2.x) + (np1.y-np2.y) * (np1.y-np2.y); If (leng1 <leng2) {// zoom-In gesture return true;} else {// zoom-out gesture return false;} // The update method enters here after the call ends and calculates the position of the camera reset function lateupdate () {// target is the box variable we bind, and if (target) for scaling and rotating the reference object {// reset the camera position y = clampangle (Y, yminlimit, ymaxlimit ); vaR rotation = Quaternion. euler (Y, X, 0); var position = rotation * vector3 (0.0, 0.0,-distance) + target. position; transform. rotation = rotation; transform. position = position ;}} static function clampangle (angle: float, Min: float, Max: Float) {If (angle <-360) angle ++ = 360; if (angle> 360) angle-= 360; return mathf. clamp (angle, Min, max );}
In untiy3d, you cannot see any effect by clicking with the mouse. You must be on the iPhone to be able to sense the effect ~

OK. Next, export unity3d TO THE xcode project. Which of the following export methods does not work ??? My first article explains this. Wow ~~ Then, run the project only on the real machine .. Look at my iPhone.






After the iPhone screen lens is rotated and scaled, this figure is not very good. I will take the projectSource codePaste the project to facilitate reading and learning ~~






It's good to rotate and scale the camera at any time. Come and learn about unity3d game development. Wow ~~

Finally, you are welcome to join Momo Discuss together Unity3d Game Development, wow ~~~ Attached to the project, the file name Is Zoom. unitypackage. After downloading, you can double-click it to automatically open it in unity3d. Then export it to an iPhone project to run it. Today, I went back to my home improvement for Windows 7 and recently became interested in WP7 game development ~ Oh, too ~ Go home and study ~ Wow



:Http://www.xuanyusong.com/archives/512


Related Article

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.