You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3d中如何使用Lean Touch实现物体绕Z轴旋转?

解决Lean Touch仅绕Z轴旋转的方案

嘿,我之前用Lean Touch做AR项目的时候也遇到过这个需求,给你几个实用的解决办法,按你的情况选就行:

方法一:直接通过Lean Rotate组件设置(最简单)

如果你的物体用的是Lean Touch自带的Lean Rotate组件,先试试这个:

  • 选中挂载了Lean Rotate的目标物体,打开Inspector面板
  • 找到Lean Rotate组件,查看是否有Axis或Restrict Rotation相关参数
  • 如果有,直接设置旋转轴为Z轴(比如选择“Z Only”或者只勾选Z轴选项)
  • 测试一下,物体应该只会绕Z轴响应旋转手势了

方法二:自定义脚本约束旋转(不修改Lean Touch源码)

如果组件本身没有轴限制选项,或者你不想改动原有组件逻辑,写个小脚本挂在目标物体上就能搞定:

using UnityEngine;
using Lean.Touch;

public class LockRotationToZ : MonoBehaviour
{
    private Vector3 originalXYRotation;

    void Awake()
    {
        // 记录物体初始的X、Y轴旋转角度,保持不变
        originalXYRotation = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
        // 订阅Lean Touch的旋转手势事件
        LeanTouch.OnRotateGesture += HandleRotateGesture;
    }

    void HandleRotateGesture(LeanRotateGesture gesture)
    {
        // 确保手势作用的是当前物体(如果用了Lean Selectable选中机制)
        if (gesture.SelectedGameObject == gameObject)
        {
            // 只更新Z轴旋转角度,X、Y轴保持初始值
            float newZRotation = transform.eulerAngles.z + gesture.DeltaDegrees;
            transform.eulerAngles = originalXYRotation + new Vector3(0, 0, newZRotation);
        }
    }

    void OnDestroy()
    {
        // 取消事件订阅,避免内存泄漏
        LeanTouch.OnRotateGesture -= HandleRotateGesture;
    }
}
  • 把这个脚本挂在需要旋转的物体上
  • 如果需要点击选中后才允许旋转,记得给物体加上Lean Selectable组件
  • 运行测试,旋转手势只会改变Z轴角度

方法三:修改Lean Touch源码(适合长期复用)

如果你想把这个功能整合到Lean Touch的默认旋转逻辑里,方便后续项目复用:

  1. 在Project面板找到Lean Touch的LeanRotate.cs脚本,双击打开
  2. 找到处理旋转增量的代码块(通常在Update或OnRotate方法里)
  3. 把原本的三维旋转增量改成只保留Z轴分量,比如:
// 原代码可能是这样的:
// Vector3 rotation = gesture.DeltaRotation * Sensitivity;
// 修改为只保留Z轴旋转:
float zRotation = gesture.DeltaDegrees * Sensitivity;
Vector3 rotation = new Vector3(0, 0, zRotation);
  1. 保存脚本后回到Unity测试,现在Lean Rotate组件就默认只会绕Z轴旋转了

内容的提问来源于stack exchange,提问作者Arigarasuthan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:38:07