Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class cPlayer {
- cameraRadius: number = 10;
- cameraSpeed: number = 0.3;
- movementSpeed: number = 10;
- cameraLength: number = 5;
- cameraAngle: number = 45;
- cameraTilt: number = 40;
- private cubeGameObject!: GameObject;
- private mainCamera!: GameObject;
- private playerObject!: GameObject;
- private rigidBody!: RigidBody;
- start(): void {
- this.cubeGameObject = new GameObject();
- this.cubeGameObject.name = "cubeObj";
- this.mainCamera = root.getObjectWithComponent("Camera");
- this.mainCamera.transform.position = new Vec3(10, 10, -10);
- this.mainCamera.transform.lookAt(new Vec3(0, 0, 0));
- this.playerObject = root.getObjectWithComponent("RigidBody");
- this.rigidBody = this.playerObject.getComponentByType("RigidBody") as RigidBody;
- }
- update(): void {
- const cameraTarget = this.playerObject.transform.position.add(new Vec3(0, 2, 0));
- this.mainCamera.transform.position = cameraTarget.add(
- calculateCameraPosition(this.cameraLength, this.cameraAngle, this.cameraTilt)
- );
- this.mainCamera.transform.lookAt(cameraTarget);
- let moveDirection = new Vec3(0, 0, 0);
- if (Input.Pressed.KEY_SPACE) {
- this.rigidBody.addForce(new Vec3(0, 1000, 0));
- }
- if (Input.Key.KEY_W) moveDirection = moveDirection.add(new Vec3(0, 0, -1));
- if (Input.Key.KEY_S) moveDirection = moveDirection.add(new Vec3(0, 0, 1));
- if (Input.Key.KEY_A) moveDirection = moveDirection.add(new Vec3(-1, 0, 0));
- if (Input.Key.KEY_D) moveDirection = moveDirection.add(new Vec3(1, 0, 0));
- if (Input.Key.KEY_Q) this.cameraAngle += 1;
- if (Input.Key.KEY_E) this.cameraAngle -= 1;
- if (Input.Mouse.Button.RIGHT) {
- if (Input.Mouse.Delta.x) {
- this.cameraAngle -= Input.Mouse.Delta.x / 5.0;
- }
- if (Input.Mouse.Delta.y) {
- this.cameraTilt += Input.Mouse.Delta.y / 5.0;
- this.cameraTilt = Utils.clamp(this.cameraTilt, -40, 89);
- }
- }
- this.cameraAngle = Utils.wrap(this.cameraAngle, 0, 360);
- if (moveDirection.length() > 0) {
- moveDirection = moveDirection.normalize();
- // rotate the direction vector by the camera rotation
- const x = moveDirection.x;
- const z = moveDirection.z;
- const rotY = (360 - this.cameraAngle) * (Math.PI / 180.0);
- const xPrime = x * Math.cos(rotY) - z * Math.sin(rotY);
- const zPrime = x * Math.sin(rotY) + z * Math.cos(rotY);
- moveDirection = new Vec3(xPrime, 0, zPrime);
- this.rigidBody.setVelocityXZ(moveDirection.mul(this.movementSpeed));
- } else {
- this.rigidBody.setVelocityXZ(new Vec3(0, 0, 0));
- }
- }
- }
- function calculateCameraPosition(cameraLength: number, cameraAngle: number, cameraTilt: number): Vec3 {
- const angleRad = (cameraAngle * Math.PI) / 180;
- const tiltRad = (cameraTilt * Math.PI) / 180;
- const x = cameraLength * Math.cos(tiltRad) * Math.sin(angleRad);
- const y = cameraLength * Math.sin(tiltRad);
- const z = cameraLength * Math.cos(tiltRad) * Math.cos(angleRad);
- return new Vec3(x, y, z);
- }
Advertisement
Add Comment
Please, Sign In to add comment