Unity3d simple implementation of object rotation and zooming movement

Source: Internet
Author: User

Because the project is running on Android, it is cumbersome to use plugins. You can change the trigger condition without a plugin.

1. Download Fingergestures plugin Click to open link


2. Import the plugin, create the scene drag the preset finger gestures Initializer to the Hierarchy view


3. Add a script and drag it onto the camera. Create a block and drag it onto the script target property.

Using unityengine;using System.collections;public class objectcontrol:monobehaviour{public Transform target;    public float yawsensitivity = 80.0f;    public float pitchsensitivity = 160.0f;    public bool Clamppitchangle = true;    public float pinchzoomsensitivity = 0.5f;//zoom speed public float smoothzoomspeed = 10.0f;    public float smoothorbitspeed = 20.0f;    public float distance = 0;    Float Yaw = 0;    float pitch = 0;    float Idealyaw = 0;    float Idealpitch = 0;    float Fchangescale = 0;    float fchangeideal = 0;    Public transform[] MOVEMENTP;    <summary>///Control Mode enumeration///</summary> public enum Controlmodel {Zoom, Rotate, Translate    } public Controlmodel Controlmodel = controlmodel.rotate;    Vector3 position=new Vector3 ();    public bool barrive = false;//Whether the mouse reaches the bounding area of the box//whether the translation mode is based on the mouse drag distance or directly to the mouse position public bool Ifdragmove = FALSE;    Translation mode: According to the mouse drag distance, the rate of public comment float movespeed = 1.0f;   Is enough to draw a button (zoom, rotate, pan) public bool ifdrawbtn = true;    Zoom to: Change camera range public bool Zoomcamera = FALSE;    Zoomcamera = True, the camera's minimum range value is public float minzoom = 0f;    Zoomcamera = True, the maximum range value of the camera public float maxzoom = 179f;    Pan object public Transform movetarget;    The initial position of the translational object Vector3 Movetargetpos;    The direct parent object of the model public Transform Parentmodel;    Vector3 Parentmodelpos;    void Start () {Zoomcamera = true;        } void Onenable () {fingergestures.ondragmove + = Fingergestures_ondragmove;        Fingergestures.onpinchmove + = Fingergestures_onpinchmove;    Fingergestures.onfingerdragend + = Onfingerdragend;        } void Ondisable () {fingergestures.ondragmove-= Fingergestures_ondragmove;        Fingergestures.onpinchmove-= Fingergestures_onpinchmove;    Fingergestures.onfingerdragend-= Onfingerdragend;        } public void Setrotation () {Vector3 angles = target.eulerangles;        Yaw = Idealyaw = Angles.y; Pitch = Idealpitch = angles.X        } void Fingergestures_ondragmove (Vector2 fingerpos, Vector2 delta) {Ondrag = true;        try {screen.showcursor = false;        } catch {screen.showcursor = false; } if (Controlmodel = = Controlmodel.rotate &&!barrive) {Idealyaw-= delta.x * Yawsensiti            Vity * 0.02F;            Idealpitch + = delta.y * pitchsensitivity * 0.02F;            len = Delta;        if (target) target.transform.Rotate (new Vector3 (DELTA.Y,-delta.x, 0), Space.world);                } if (Controlmodel = = Controlmodel.translate &&!barrive) {if (ifdragmove) {  if (Movetarget = = null) {target.position = new Vector3 (target.position.x                + delta.x * movespeed, TARGET.POSITION.Y + delta.y * movespeed, TARGET.LOCALPOSITION.Z);//Getworldpos (FingerPos); } else {moveTarget.position = new Vector3 (movetarget.position.x + delta.x * movespeed, MOVETARGET.POSITION.Y + delta.y * moveSpeed, MO                VETARGET.LOCALPOSITION.Z); }} else {if (Movetarget = = null) {tar                Get.position = Getworldpos (Fingerpos);                } else {movetarget.position = Getworldpos (Fingerpos);        }}}} void Fingergestures_onpinchmove (Vector2 fingerPos1, Vector2 fingerPos2, float delta) {                if (Controlmodel = = Controlmodel.zoom &&!barrive) {if (Zoomcamera) {                float fzoom = Camera.fieldofview-delta * pinchzoomsensitivity * time.deltatime;                Fzoom = Mathf.min (Fzoom, maxzoom);                Fzoom = Mathf.max (Fzoom, minzoom); Camera.fieldofview = Mathf.lerp (Camera.fieldofview, Fzoom, Time.deltatime * smoothzoomSpeed);            camera.transform.position = Target.position-fzoom * Camera.transform.forward;                } else {Fchangescale = target.localscale.x + Delta * pinchzoomsensitivity;            Vector3 VC = new Vector3 (Fchangescale, Fchangescale, Fchangescale); }}}//sliding end void Onfingerdragend (int fingerindex, Vector2 fingerpos) {Screen.showcursor = True        ;    Ondrag = false;        }//Convert Unity screen coordinates to Vector3 Getworldpos (Vector2 screenpos) {//Camera Maincamera = Camera.main; Camera Maincamera = Gameobject.findgameobjectwithtag ("Maincamera").        Getcomponent<camera> (); if (!maincamera.enabled) {Maincamera = MainCamera.transform.parent.FindChild ("Cameraone").        Getcomponent<camera> (); } return Maincamera.screentoworldpoint (New Vector3 (Screenpos.x, Screenpos.y, Mathf.abs (target.position.z-maincame    (RA.TRANSFORM.POSITION.Z))); }    void Apply () {if (Controlmodel = = Controlmodel.rotate &&!barrive) {yaw = Mathf.            Lerp (Yaw, Idealyaw, Time.deltatime * smoothorbitspeed);        Pitch = Mathf.lerp (pitch, Idealpitch, time.deltatime * smoothorbitspeed);    }} bool Ondrag;    Vector2 Len; void Lateupdate () {if (Input.getmousebuttonup (1) | | |        Input.getmousebuttonup (0)) {screen.showcursor = true;    } Apply ();        } static float Clampangle (float angle, float min, float max) {if (angle < -360) angle + = 360;        if (angle > Angle)-= 360;    return Mathf.clamp (angle, Min, max); } void Update () {///free Toggle if (Input.getmousebuttondown (0)) {Controlmodel = Controlm Odel.        Translate;        } if (Input.getmousebuttondown (1)) {Controlmodel = controlmodel.rotate; } if (Input.getaxis ("Mouse scrollwheel")! = 0)        {Controlmodel = Controlmodel.zoom; }}///<summary>//Reset//</summary> public void Resetvalue () {if (Movetarget! =        NULL) {movetarget.localposition = Movetargetpos;        } if (Parentmodel! = null) {parentmodel.localposition = Parentmodelpos;        } yaw = 0;        Pitch = 0;        Idealyaw = 0;    Idealpitch = 0; }}


Unity3d simple implementation of object rotation and zooming movement

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.