Unity 拖动分针实时跟随,时针跟随转动

本文介绍了一个Unity脚本,该脚本允许用户通过鼠标拖动来改变表盘上分针的位置,并使时针根据分针的变化进行相应调整。通过计算鼠标位置与屏幕中心的角度来确定分针的角度,并确保时针正确反映当前的时间比例。

鼠标拖动分针转动,时刻跟随,即指针一直在鼠标下,时针按1:12随分针转动。

代码:

/************************************************************
  FileName: Watch.cs
  Author:末零       Version :1.0          Date: 2018-7-19
  Description:表针拖动
************************************************************/

using UnityEngine;

public class Watch: MonoBehaviour {

    public float speed;
    public Transform pointerM;
    public Transform pointerH;

    private float angleM;
    private Vector3 oldVector_M;

    public void DrugEvent()
    {
        oldVector_M = pointerM.localEulerAngles;

        //通过鼠标位置决定角度(因为Vector3.Angle不会大于180)
        if (Input.mousePosition.x >= Screen.width / 2)
        {
            angleM = 360 - Vector3.Angle(new Vector3(0, 1, 0), new Vector3(Input.mousePosition.x - Screen.width / 2, Input.mousePosition.y - Screen.height / 2, 0));
        }
        else
        {
            angleM = Vector3.Angle(new Vector3(0, 1, 0), new Vector3(Input.mousePosition.x - Screen.width / 2, Input.mousePosition.y - Screen.height / 2, 0));
        }

        pointerM.localEulerAngles = new Vector3(0, 0, angleM);

        if (Mathf.Abs(pointerM.localEulerAngles.z - oldVector_M.z) < 180)//判断是否经过12
        {
            pointerH.localEulerAngles += new Vector3(0, 0, (pointerM.localEulerAngles.z - oldVector_M.z) / 12);
        }
        else
        {
            if (Input.mousePosition.x > Screen.width / 2)//顺时针经过
            {
                pointerH.localEulerAngles += new Vector3(0, 0, (pointerM.localEulerAngles.z - oldVector_M.z - 360) / 12);
            }
            else//逆时针经过
            {
                pointerH.localEulerAngles += new Vector3(0, 0, (pointerM.localEulerAngles.z - oldVector_M.z + 360) / 12);
            }
        }
    }
}

 

Unity中使用`OnDrag`方法实现物体拖拽实时跟随坐标有多种情况,以下为不同场景的实现方法: ### UI 物体拖拽 对于UI物体的拖拽,可使用`EventSystem`和相关接口来实现。在拖拽过程中,需将屏幕坐标转换为UGUI坐标。以下是一个示例代码: ```csharp using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class UIDrag : MonoBehaviour, IDragHandler { public Camera UICamera; public RectTransform targetTransform; public void OnDrag(PointerEventData eventData) { Vector2 pos; RectTransformUtility.ScreenPointToLocalPointInRectangle(targetTransform, eventData.position, UICamera, out pos); targetTransform.localPosition = pos; } } ``` 此代码实现了UI物体在拖拽时实时跟随鼠标坐标移动,其中`ScreenPointToLocalPointInRectangle`方法用于将屏幕坐标转换为UGUI坐标[^1]。 ### 模型物体拖拽 对于模型物体的拖拽,可通过继承相关接口实现。以下是一个基类示例: ```csharp using UnityEngine; using UnityEngine.EventSystems; public abstract class BaseDrag : MonoBehaviour, IDragHandler { [Header("是否精准拖拽!")] public bool m_isPrecision = true; // 偏移量 [SerializeField] protected Vector3 offset = Vector3.zero; public void OnDrag(PointerEventData eventData) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(eventData.position); if (Physics.Raycast(ray, out hit)) { transform.position = hit.point + offset; } } } ``` 该基类实现了模型物体在拖拽时实时跟随鼠标坐标移动,通过射线检测获取鼠标点击位置,并更新物体的位置[^2]。 ### 2D 物体旋转拖拽 对于2D物体的旋转拖拽,可在`OnDrag`方法中实现旋转逻辑。以下是一个示例代码: ```csharp using UnityEngine; using UnityEngine.EventSystems; namespace Kernal { public class RotateImage : MonoBehaviour, IDragHandler { public void OnDrag(PointerEventData eventData) { SetDraggedRotation(eventData); } private void SetDraggedRotation(PointerEventData eventData) { Vector2 curSelfScreenPosition = RectTransformUtility.WorldToScreenPoint(eventData.pressEventCamera, transform.position); Vector2 directionTo = curSelfScreenPosition - eventData.position; Vector2 directionFrom = directionTo - eventData.delta; this.transform.rotation *= Quaternion.FromToRotation(directionTo, directionFrom); } } } ``` 此代码实现了2D物体在拖拽时实时旋转,通过计算方向向量并使用`Quaternion.FromToRotation`方法实现旋转[^3]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

末零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值