Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pc.script.create('dynamic_character_controller', function (context) {
- var SPEED = 5;
- var JUMP_IMPULSE = 400;
- var origin = pc.Vec3(0,0,0);
- var groundCheckRay = pc.Vec3(0, -0.51, 0);
- var rayEnd = pc.Vec3(0,0,0);
- var DynamicCharacterController = function (entity) {
- this.entity = entity;
- this.speed = SPEED;
- this.jumpImpulse = pc.Vec3(0, JUMP_IMPULSE, 0);
- this.onGround = true;
- };
- DynamicCharacterController.prototype = {
- initialize: function () {
- // Prevent any rotation of the rigid body
- this.entity.rigidbody.angularFactor = 0;
- },
- update: function (dt) {
- this._checkGround();
- },
- /**
- * Move the character in the direction supplied
- */
- move: function (direction) {
- if (this.onGround) {
- this.entity.rigidbody.activate();
- pc.Vec3.scale(direction, this.speed, direction);
- this.entity.rigidbody.linearVelocity = pc.Vec3(direction[0], direction[1], direction[2]);
- }
- },
- /**
- * Make the character jump
- */
- jump: function () {
- if (this.onGround) {
- this.self.rigidbody.activate();
- this.self.rigidbody.applyImpulse(this.jumpImpulse, origin);
- this.onGround = false;
- }
- },
- /**
- * Check to see if the character is standing on something
- */
- _checkGround: function () {
- var self = this;
- var pos = this.entity.getPosition();
- pc.Vec3.add(pos, groundCheckRay, rayEnd);
- self.onGround = false;
- // Fire a ray straight down to just below the bottom of the rigid body,
- // if it hits something then the character is standing on something.
- context.systems.rigidbody.raycastFirst(pos, rayEnd, function (result) {
- self.onGround = true;
- });
- }
- };
- return DynamicCharacterController;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement