import { Component, v3, _decorator, Node } from "cc"; const { ccclass, property, executeInEditMode } = _decorator; /** * 跟随脚本,挂载被跟随的节点上 */ @ccclass @executeInEditMode export class Follow extends Component { //需要跟随的目标节点数组 @property(Node) private targets: Node[] = []; _pos = v3(); _isPause = false; protected update(dt: number): void { this._pos = this.node.position; } protected lateUpdate(dt: number): void { if (!this._isPause) { this.move() } } setTargets(targets: Node[]) { this.targets = targets; } pause() { this._isPause = true; } resume() { this._isPause = false; } move() { let p = this.node.position; for (let i = 0; i < this.targets.length; i++) { let t = this.targets[i].position; t.add3f(p.x - this._pos.x, p.y - this._pos.y, p.z - this._pos.z); this.targets[i].position = t; } } }