UI整体根据鼠标或手指在屏幕中的位置进行偏转


需要将渲染这个元素的Canvas选择一个可以指定相机的模式(Camera或者World Space)
然后指定一个透视的相机去渲染这个Canvas,之后将下面的脚本挂载到要控制旋转的物体身上

using UnityEngine;

public class RoteFollowMouse : MonoBehaviour
{
    public Vector2 range = new Vector2(5f, 3f);

    Transform mTrans;
    Quaternion mStart;
    Vector2 mRot = Vector2.zero;

    void Start ()
    {
        mTrans = transform;
        mStart = mTrans.localRotation;
    }

    void Update ()
    {
        Vector3 pos = Input.mousePosition;

        float halfWidth = Screen.width * 0.5f;
        float halfHeight = Screen.height * 0.5f;
        float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
        float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
        mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);

        mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
    }
}
发布了127 篇原创文章 · 获赞 278 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/LLLLL__/article/details/103911136