ModelRotator.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { _decorator, Component, EventTouch, Input, input, Node, quat, Quat, tween, Tween, Vec3 } from 'cc';
  2. import { uiMgr } from '../core/manager/UIManager';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('ModelRotator')
  5. export class ModelRotator extends Component {
  6. @property({
  7. tooltip: '初始旋转速度(度/秒)'
  8. })
  9. speed: number = 45;
  10. @property({
  11. tooltip: '触摸后的减速系数(0-1)'
  12. })
  13. touchDeceleration: number = 0.95;
  14. @property({
  15. tooltip: '斜角旋转的X轴角度(度)'
  16. })
  17. tiltAngle: number = 36; // 改为36度斜角
  18. // 新增Canvas节点引用
  19. private canvasNode: Node = null!;
  20. private _targetQuat: Quat = quat();
  21. private _currentSpeed: number = 0;
  22. private _touchStartX: number = 0;
  23. private _isDragging: boolean = false;
  24. private _baseRotation: Vec3 = new Vec3();
  25. start() {
  26. this.canvasNode = this.node.parent.parent;
  27. // 设置斜角旋转(X轴36度 + Z轴旋转)
  28. this._baseRotation.set(this.tiltAngle, 0, -this.tiltAngle); // 增加Z轴倾斜
  29. this.node.setRotationFromEuler(this._baseRotation);
  30. // 删除冗余的全局input监听
  31. this.canvasNode.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
  32. this.canvasNode.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  33. this.canvasNode.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  34. // 初始化自动旋转
  35. this._currentSpeed = this.speed;
  36. this.startAutoRotation();
  37. }
  38. startAutoRotation() {
  39. tween(this.node)
  40. .by(1, { //改为绕Z轴旋转
  41. eulerAngles: new Vec3(this._currentSpeed, 0, -this._currentSpeed)
  42. })
  43. .repeatForever()
  44. .start();
  45. }
  46. onTouchMove(event: EventTouch) {
  47. if (!this._isDragging) return;
  48. // 改进差值计算方式
  49. const currentX = event.getUILocation().x;
  50. const deltaX = currentX - this._touchStartX;
  51. this._touchStartX = currentX;
  52. // 改为同时更新Z轴旋转
  53. const currentEuler = this.node.eulerAngles.clone();
  54. currentEuler.z += deltaX * 0.5; // 绕Z轴旋转
  55. this.node.setRotationFromEuler(
  56. new Vec3(
  57. this.tiltAngle, // 保持X轴斜角
  58. 0, // 固定Y轴
  59. currentEuler.z // Z轴旋转
  60. )
  61. );
  62. }
  63. onTouchStart(event: EventTouch) {
  64. this._touchStartX = event.getLocationX();
  65. this._isDragging = true;
  66. this._currentSpeed = 0; // 停止自动旋转
  67. Tween.stopAllByTarget(this.node);
  68. }
  69. onTouchEnd() {
  70. this._isDragging = false;
  71. // 添加惯性滑动效果
  72. this._currentSpeed = this.speed * this.touchDeceleration;
  73. this.startAutoRotation();
  74. }
  75. onDestroy() {
  76. //改为Canvas节点的事件解绑
  77. if (this.canvasNode) {
  78. this.canvasNode.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
  79. this.canvasNode.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  80. this.canvasNode.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  81. }
  82. }
  83. }