33 lines
986 B
TypeScript
33 lines
986 B
TypeScript
// assets/scripts/CameraRig.ts
|
||
import { _decorator, Component, Node, Vec3, math } from "cc";
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass("CameraRig")
|
||
export class CameraRig extends Component {
|
||
@property(Node) ball!: Node;
|
||
@property({ tooltip: "跟随平滑(0~1,越小越平滑)" }) smooth = 0.1;
|
||
@property(Vec3) offset = new Vec3(0, 2.5, -5);
|
||
|
||
private _mode: "static" | "follow" = "static";
|
||
|
||
setModeFollow() {
|
||
this._mode = "follow";
|
||
}
|
||
setModeStatic() {
|
||
this._mode = "static";
|
||
}
|
||
|
||
lateUpdate(dt: number) {
|
||
if (this._mode !== "follow" || !this.ball) return;
|
||
const target = this.ball.worldPosition.clone().add(this.offset);
|
||
const p = this.node.worldPosition.clone();
|
||
const lerp = new Vec3(
|
||
math.lerp(p.x, target.x, 1 - this.smooth),
|
||
math.lerp(p.y, target.y, 1 - this.smooth),
|
||
math.lerp(p.z, target.z, 1 - this.smooth)
|
||
);
|
||
this.node.setWorldPosition(lerp);
|
||
this.node.lookAt(this.ball.worldPosition);
|
||
}
|
||
}
|