Unity 拖动鼠标旋转模型(类似时钟拖动指针)

Unity 拖动鼠标旋转模型(类似时钟拖动指针)_第1张图片 

拖动指针在圆中旋转

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MoveCompass : MonoBehaviour
{
    private bool isPress;//是否按下

    private Vector3 startPos;//开始位置
    private Vector3 endPos;//结束位置
    public float dis;//距离

    private void Update()
    {
        startPos = Input.mousePosition;
        if (isPress)
        {
            Vector3 offset = endPos - startPos;
            transform.Rotate(Vector3.back * Time.deltaTime * offset.sqrMagnitude * 500);
        }
        endPos = Input.mousePosition;
    }

    public void ClickImage()
    {
        isPress = true;

    }

    public void UnClick()
    {
        isPress = false;
        this.transform.parent.parent.GetComponentInChildren().text = Math.Round(360 - this.transform.localEulerAngles.z).ToString();
//        Debug.Log(this.transform.rotation);
    }
}

其中,修改transform.rotation的两种方法

transform.localPosition和transform.localScale都是直接赋值三元数,给旋转赋值需要用 
  方法一: 
  xxx.transform.localEulerAngles = new Vector3 (0.0f,0.0f,0.0f); 
  方法二: 
   xxx.transform.rotation=Quaternion.Euler(0.0f,0.0f,0.0f);

 

你可能感兴趣的:(unity)