Files
ball/assets/scripts/CameraRig.ts

33 lines
986 B
TypeScript
Raw Normal View History

1970-01-01 08:45:37 +08:00
// 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);
}
}