FollowCamera2D.ts 558 B

123456789101112131415161718192021222324252627
  1. import { _decorator, Component, Node, Vec3, v3, Camera } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. const tmpV3 = v3();
  4. @ccclass('FollowCamera2D')
  5. export class FollowCamera2D extends Component {
  6. @property(Node)
  7. target:Node;
  8. @property
  9. offset:Vec3 = v3();
  10. protected _camera:Camera;
  11. start() {
  12. this._camera = this.node.getComponent(Camera);
  13. }
  14. lateUpdate(deltaTime: number) {
  15. this.target.getWorldPosition(tmpV3);
  16. tmpV3.add(this.offset);
  17. this.node.worldPosition = tmpV3;
  18. }
  19. }