import { Component, v3, Vec3, _decorator, Node } from "cc"; const { ccclass, property, executeInEditMode } = _decorator; /** * 跟随脚本,挂载跟随的节点上(单个节点保持偏移的方式) */ @ccclass //@executeInEditMode export class FollowOffset extends Component { @property(Node) private target: Node = null; @property(Vec3) private offset: Vec3 = v3(); @property private offsetAngle: number = 0; _isPause = false; // oldParent: Node = null; // protected onEnable(): void { // this.oldParent = this.node.parent; // } // protected onDisable(): void { // this.node.parent = this.oldParent; // } protected update(dt: number): void { if (this.target.isValid && !this._isPause) { this.updatePos(); } } updatePos() { let p = this.target.worldPosition; this.node.worldPosition = v3(p.x + this.offset.x, p.y + this.offset.y, p.z + this.offset.z); this.node.angle = this.target.angle + this.offsetAngle; } setTarget(target: Node, offset: Vec3 = null) { this.target = target; this.updatePos(); if (offset) this.offset = offset; } pause() { this._isPause = true; } resume() { this._isPause = false; } }