-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathStartPosition.ts
42 lines (33 loc) · 1.02 KB
/
StartPosition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// START MARKER Auto Reset
import { Behaviour, Collider, GameObject, Rigidbody, serializeable } from "@needle-tools/engine";
import { Vector3 } from "three";
export class StartPosition extends Behaviour {
//@nonSerialized
startPosition?: Vector3;
start() {
this.updateStartPosition();
}
updateStartPosition(){
this.startPosition = this.gameObject.position.clone();
}
resetToStart() {
if (!this.startPosition) return;
const rb = GameObject.getComponent(this.gameObject, Rigidbody);
rb?.teleport(this.startPosition);
}
}
/** Reset to start position when object is exiting the collider */
export class AutoReset extends StartPosition {
@serializeable(Collider)
worldCollider?: Collider;
start(){
super.start();
if(!this.worldCollider) console.warn("Missing collider to reset", this);
}
onTriggerExit(col) {
if(col === this.worldCollider){
this.resetToStart();
}
}
}
// END MARKER Auto Reset