【问题标题】:Unity3D - Rotate image in XY axis when mouse overUnity3D - 鼠标悬停时沿 XY 轴旋转图像
【发布时间】:2019-04-15 15:27:25
【问题描述】:

我想知道当它像这样悬停时如何旋转图像:

https://drive.google.com/open?id=14_fxL8snv71yTd3F80Z6CsPeLKUQoco0

https://i.stack.imgur.com/CJu9S.jpg

我尝试了很多不同的选项,但都失败了。

Coroutine lastRoutine = null;

    public void OnPointerEnter(PointerEventData data)
    {
        lastRoutine = StartCoroutine(Test());
    }

        IEnumerator Test()
    {
        while (true)
        {
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 0f;

            Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
            mousePos.x = objectPos.x - mousePos.x;
            mousePos.y = objectPos.y - mousePos.y;

            float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.Euler(new Vector3(angle, angle, 0));

        yield return null;
        }
    }

    public void OnPointerExit(PointerEventData data)
    {
        StopCoroutine(lastRoutine);
    }

【问题讨论】:

    标签: c# unity3d image-rotation


    【解决方案1】:

    这就是我将如何处理您正在尝试做的事情:

    public class MouseEffect : MonoBehaviour
    {
        public float zOffset = -2f;
        public Vector2 tolerance = Vector2.one;
        Quaternion originalRotation;
    
        private void OnMouseEnter()
        {
            // Storing the original rotation so we can reset to it when we aren't hovering anymore
            originalRotation= transform.rotation;
        }
    
        private void OnMouseOver()
        {
            Vector3 localOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    
            // Multiplying by a adjustable tolerance, this is just a matter of preference if you want more rotation on the xAxis/yAxis or not.
            localOffset.x *= tolerance.x;
            localOffset.y *= tolerance.y;
            Vector3 worldOffset = transform.position + localOffset;
    
            // Setting a zOffset it will be really weird to adjust the rotation to look at something on the same Z as it.
            worldOffset.z = transform.position.z + zOffset;
    
            // Transform.LookAt for simplicity sake.
            transform.LookAt(worldOffset);
        }
    
        private void OnMouseExit()
        {
            // This can cause a bit of a twitching effect if you are right on the edge of the collider.
            transform.rotation = originalRotation;
        }
    }
    

    【讨论】:

    • 谢谢!这几乎是我需要的。但也有一些问题:从上往下翻是错误的,并且由于 originalRotation = transform.rotation 和 transform.rotation = originalRotation 出现了一个小故障。看:drive.google.com/open?id=1F23-KUxIKR0HxiCTm2K91m6GtvvbJPtL我该如何解决这个问题?
    • @AlexPunto 我在评论中提到了 OnMouseExit 上的故障,有几种方法可以调整它,您可以将所有这些代码放入更新中,并存储对象的原始边界,如果鼠标在你做 OnMouseOver 的那些范围内,你仍然会有轻微的故障,我建议使用协程和 quaternion.slerp,以“快速”将对象移回其原始位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多