物体拖拽与旋转

物体拖拽与旋转

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class WXC_ObjectDrag : MonoBehaviour
{
    public float xMove = 5;
    public float yMove = 5;
    public float wheelSpeed =0.005f;
    public float minBeiShu;
    public float maxBeiShu;
    public float move_Speed;
    Vector3 sca;
    Vector3 pos;
    Quaternion rot;
    public static bool isTrue = true;
    public static bool isOn = true;
    // Use this for initialization
    Vector3 vecDown;
    Vector3 vecScroll;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (this.gameObject.activeSelf == true)
        {
            if (Input.GetMouseButtonDown(0))
            {
                vecDown = Input.mousePosition;
            }
            if (Input.GetMouseButtonDown(2))
            {
                vecScroll = Input.mousePosition;
            }
            if (Input.GetMouseButton(0))//点击鼠标左键旋转物体
            {
                if (isTrue == true && isOn == true)
                {
                    Vector3 deltaPos = Input.mousePosition - vecDown;
                    Vector3 vec = deltaPos.normalized;
                    this.transform.RotateAround(this.transform.position, -Vector3.up, vec.x * xMove); // 鼠标横向输入
                    this.transform.RotateAround(this.transform.position, Vector3.right, vec.y * yMove); //鼠标纵向输入
                }

            }
            if (isTrue == true && isOn == true)//滚动滑轮,缩放物体大小
            {
                //Vector2 suoF = Input.mouseScrollDelta;
                //Vector3 suoFang = new Vector3(suoF.y,suoF.y,suoF.y);
                //transform.localScale += suoFang*0.005f;
                transform.localScale += new Vector3(Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed);
            }
            if (Input.GetMouseButton(2))//按住滚轮拖拽物体移动
            {
                Vector3 scPos = Input.mousePosition - vecScroll;
                Vector3 vec1 = scPos.normalized;
                transform.Translate(vec1.x * move_Speed, vec1.y * move_Speed, 0, Space.World);
            }

            if (transform.localScale.x < sca.x * minBeiShu)
            {
                transform.localScale = sca * minBeiShu;
            }
            else if (transform.localScale.x > sca.x * maxBeiShu)
            {
                transform.localScale = sca * maxBeiShu;
            }
        }

    }
    void OnEnable()
    {
        isTrue = true;
        sca = transform.localScale;
        pos = transform.localPosition;
        rot = transform.localRotation;
    }
    void OnDisable()
    {
        transform.localPosition = pos;
        transform.localScale = sca;
        transform.localRotation = rot;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值