unity3d andorid 滑动手指旋转模型 (2):增加拖拽模型

在手指滑动旋转模型的基础上 ,增加点击模型拖拽模型
话不多说 附代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DeviceModelMove : MonoBehaviour
{
    private Vector3 startFingerPos;
    private Vector3 nowFingerPos;
    private float xMoveDistance;
    private float yMoveDistance;
    private int xbackValue = 0;
    private int ybackValue = 0;
    public GameObject my_Cube;
    public float RotateSpeed = 300f;

    private bool isdrag = false;
    Vector3 targetPos;
    // Update is called once per frame
    void Update()
    {
        movemodel();
        rotatemodel();

    }

    private void movemodel()
    {
        if (Input.GetMouseButton(0))
        {
            if (!isdrag)
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.name == my_Cube.transform.name)
                    {
                        Debug.Log("第一个点点的模型  要拖拽");
                        isdrag = true;
                    }
                }
            }
            if (isdrag)
            {
                Debug.Log("更新模型位置 Input.mousePosition  = " + Input.mousePosition.ToString());
                targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
                Debug.Log("更新模型位置 targetPos  = " + targetPos.ToString());
                my_Cube.transform.position = new Vector3(targetPos.x, targetPos.y, 0.1f);
            }
        }
        else {
            isdrag = false;
        }
    }

    private void rotatemodel()
    {
        //没有触摸
        if (isdrag || Input.touchCount <= 0)
        {
            return;
        }
        if (Input.GetTouch(0).phase == TouchPhase.Began)
        {
            //Debug.Log("======开始触摸=====");
            startFingerPos = Input.GetTouch(0).position;
        }

        nowFingerPos = Input.GetTouch(0).position;

        if ((Input.GetTouch(0).phase == TouchPhase.Stationary) || (Input.GetTouch(0).phase == TouchPhase.Ended))
        {

            startFingerPos = nowFingerPos;
            //Debug.Log("======释放触摸=====");
            return;
        }
        if (startFingerPos == nowFingerPos)
        {
            return;
        }
        xMoveDistance = Mathf.Abs(nowFingerPos.x - startFingerPos.x);
        yMoveDistance = Mathf.Abs(nowFingerPos.y - startFingerPos.y);
        if (nowFingerPos.x - startFingerPos.x > 0)
        {
            xbackValue = -1; //沿着X轴负方向移动
        }
        else
        {
            xbackValue = 1; //沿着X轴正方向移动
        }
        if (nowFingerPos.y - startFingerPos.y > 0)
        {
            ybackValue = 1; //沿着Y轴正方向移动
        }
        else
        {
            ybackValue = -1; //沿着Y轴负方向移动
        }
        my_Cube.transform.Rotate((xbackValue * Vector3.up * (xMoveDistance / (xMoveDistance + yMoveDistance))) + (ybackValue * Vector3.right * (yMoveDistance / (xMoveDistance + yMoveDistance)))
           * Time.deltaTime * RotateSpeed, Space.World);

    }


}

欢迎阅读~~
may the foce be with you

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值