首页 > Unity3D专栏 > 【Unity3D】Unity3D基于移动端的旋转、缩放功能
2017
08-01

【Unity3D】Unity3D基于移动端的旋转、缩放功能

其中Obj就是物体。

把脚本挂到相机上,

然后给obj赋值即可。

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{

    public GameObject Obj;
    private int MouseWheelSensitivity = 1;
    private int MouseZoomMin = 1;
    private int MouseZoomMax = 5;
    private float normalDistance = 3;

    private Vector3 normalized;

    private float xSpeed = 250.0f;
    private float ySpeed = 120.0f;

    private int yMinLimit = -20;
    private int yMaxLimit = 80;

    private float x = 0.0f;
    private float y = 0.0f;

    private Vector3 screenPoint;
    private Vector3 offset;

    //private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));
    private Vector3 CameraTarget;

    public float scale = 0.5f;

    private Vector2 oldPosition1;
    private Vector2 oldPosition2;


    public void Start()
    {

        
        CameraTarget = Obj.transform.position;

        float z = Obj.transform.position.z - normalDistance;
        //transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);

        //transform.LookAt(Obj.transform);

        var angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }

    void Update()
    {

        if (Input.touchCount == 1)
        {
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                    y = ClampAngle(y, yMinLimit, yMaxLimit);

                    var rotations = Quaternion.Euler(y, x, 0);
                    var positions = rotations * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;

                    transform.rotation = rotations;
                    transform.position = positions;
                }
        }



        if (Input.touchCount > 1)
        {

            if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
            {

                Vector2 tempPosition1 = Input.GetTouch(0).position;
                Vector2 tempPosition2 = Input.GetTouch(1).position;
                if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                {
                    //放大
                    normalDistance -= 0.05f;
                }
                else
                {
                    //缩小
                    normalDistance += 0.05f;
                }
                oldPosition1 = tempPosition1;
                oldPosition2 = tempPosition2;
            }
        }
        transform.LookAt(CameraTarget);
        var rotation = Quaternion.Euler(y, x, 0);
        var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + Obj.transform.position;
        transform.rotation = rotation;
        transform.position = position;

    }
    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1 < leng2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}

 



虚拟世界提醒您:如果您觉得本文不错,快快将这篇文章分享出去吧 ,感谢您的支持和关注,谢谢!
最后编辑:
作者:虚拟世界
专注互联网技术及资讯 技术研究中心|有相关Unity3D问题或者作事宜请联系我哦!
捐 赠如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝或者微信直接向我捐款,在此非常感谢您对虚拟世界的捐赠。

留下一个回复

你的email不会被公开。