Unity3D-鼠标悬停在XY轴上旋转图像

时间:2019-04-15 15:27:25

标签: c# unity3d image-rotation

我想了解如何在这样悬停图像时旋转图像:

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

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

我尝试了很多不同的选项,但是效果不佳。

from datetime import datetime, timedelta

base = datetime(2018, 8, 1)

for d in range(101):
    date = base + timedelta(days=d)
    print(d, date.strftime('%d-%m-%Y'))

1 个答案:

答案 0 :(得分:0)

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

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;
    }
}