不忘初心 方得始终

愿你一生努力 。一生被爱 。想要的都拥有,得不到的都释怀 。

Unity3D 拖动物体旋转和缩放

using UnityEngine;
using System.Collections;

public class ThisRotate : MonoBehaviour
{
    Vector3 StartPosition;
    Vector3 previousPosition;
    Vector3 offset;
    Vector3 finalOffset;
    Vector3 eulerAngle;

    bool isSlide;
    float angle;

    public float scale = 1;

    RaycastHit hit;

    void Start()
    {

    }

    void OnGUI()
    {
        
        Ray ray =Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, out hit))
        {
            if (hit.transform == transform)
            {
                if (Input.GetAxis("Mouse ScrollWheel") != 0)
                {
                    Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
                    scale = scale + Input.GetAxis("Mouse ScrollWheel");
                    transform.localScale = new Vector3(1 + scale, 1 + scale, 1 + scale);
                }
            }
        }
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            StartPosition = Input.mousePosition;
            previousPosition = Input.mousePosition;
        }
        if (Input.GetMouseButton(0))
        {
            offset = Input.mousePosition - previousPosition;
            previousPosition = Input.mousePosition;
            transform.Rotate(Vector3.Cross(offset, Vector3.forward).normalized, offset.magnitude, Space.World);

        }
        if (Input.GetMouseButtonUp(0))
        {
            finalOffset = Input.mousePosition - StartPosition;
            isSlide = true;
            angle = finalOffset.magnitude;


        }
        if (isSlide)
        {
            transform.Rotate(Vector3.Cross(finalOffset, Vector3.forward).normalized, angle * 2 * Time.deltaTime, Space.World);
            if (angle > 0)
            {
                angle -= 5;
            }
            else
            {
                angle = 0;
            }
        }
    }
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AWNUXCVBN/article/details/17394025
文章标签: c# Unity3D
个人分类: Unity
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭