Unity3d in the mobile phone to zoom in and out of the lens with gestures

Source: Internet
Author: User

Just entered a company, on the 4-day class, finally found that they are so dishes, read the eldest brother wrote some code. No way to look down, it is too strong, in these days to work, because it is a very big company, feel pressure is very big ah. The heart is very depressed. Because I am very food. All right, let's not talk about this, come on. Keep on studying hard. These days, the boss told me to achieve in the mobile phone to achieve zoom in and move the scene, a bit like the island of the same control lens effect, in writing these code. Have encountered a lot of problems. Because of the tension, but also because of their own foundation.


The code below, you need to bind this script to the camera:



Using Unityengine;
Using System.Collections;

public class Cameracontroller:monobehaviour
{

public float zoomspeed = 500;
public float Zoomspeed
{
Get
{
return zoomspeed;
}
Set
{
Zoomspeed = value;
}
}
public float Maxy = 3921;
public float Maxy
{
Get
{
return Maxy;
}
Set
{
Maxy = value;
}
}

public float miny = 1200;
public float Miny
{
Get
{
return miny;
}
Set
{
Miny = value;
}
}
Update is called once per frame
Private Vector2 lastTouchPos1 = Vector2.zero;
Private Vector2 lastTouchPos2 = Vector2.zero;


void Update ()
{
Here is the mouse control
if (Input.getaxis ("Mouse scrollwheel")! = 0)
{
var forward = Camera.main.transform.forward * Zoomspeed * Input.getaxis ("Mouse scrollwheel");
Camera.main.transform.position + = forward;
}

Here's the gesture control on the phone
if (Input.touchcount > 1)
{

if (Input.gettouch (0). Phase = = Touchphase.moved | | Input.gettouch (1). Phase = = touchphase.moved)
{
Vector2 v1 = Input.gettouch (0). Position;
Vector2 v2 = Input.gettouch (1). Position;



float distance = getdistance (v1, v2);
Vector3 forward = Camera.main.transform.forward * Distance * time.deltatime * 0.5f;
Vector3 Newpos = Camera.main.transform.position + forward;
if (Newpos.y > Maxy | | newpos.y < miny)
{

}
Else
{
Camera.main.transform.position = Newpos;
}
LASTTOUCHPOS1 = Input.gettouch (0). Position;
LastTouchPos2 = Input.gettouch (1). Position;

}
Else
{
LASTTOUCHPOS1 = Vector2.zero;
LastTouchPos2 = Vector2.zero;
}


}

}


Private float getdistance (Vector2 pos1, Vector2 pos2)
{
if (lastTouchPos1 = = Vector2.zero && LastTouchPos2 = = Vector2.zero)
{
return 0.0f;
}

Return (POS2-POS1). Sqrmagnitude-(LASTTOUCHPOS2-LASTTOUCHPOS1). Sqrmagnitude;
}

}

This is just to achieve zoom out of the lens, the next time to fill the mobile scene. Work overtime, everybody, come on.

This article is from "Cool Little Joe" blog, please be sure to keep this source http://5152481.blog.51cto.com/5142481/1619861

Unity3d in the mobile phone to zoom in and out of the lens with gestures

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.