Hi, thanks in advance for the help, I’m trying to make a game where you can basically play basketball, the main thing is that I need the ball to teleport back to the centre of the field when is leaves a trigger within each hoop,
A link to my project:
Basketball Project
This is the simple code I’ve written for that:
var Teleport = pc.createScript('teleport');
// initialize code called once per entity
Teleport.prototype.initialize = function () {
this.entity.collision.on('triggerleave', this.onTriggerLeave, this);
};
Teleport.prototype.onTriggerleave = function () {
console.log('should teleport');
this.ball = this.app.root.findByName('Basketball');
var teleportPoint = this.app.root.findByName('TeleportPoint').getPosition();
this.ball.setPosition(teleportPoint);
};
It doesn’t seem to work in that nothing happens.
I would also like the code to change a text object by +1 on the basketball triggering it, but I’m not sure how this would be done?
Finally, I’ve used a Drag and Drop script from someone’s demo project and modified it slightly, but at first and now, it doesn’t work seemingly when I’m using a trackpad? My device also has touchscreen. Also while working more often it seems to change with every launch on kbm(I’m aware this could be difficult to help with).
Drag and Drop script:
var DragAndDrop = pc.createScript('dragAndDrop');
DragAndDrop.attributes.add('pickableDistance', {
type: 'number',
default: 10
});
DragAndDrop.attributes.add('throwPower', {
type: 'number',
default: 10
});
// initialize code called once per entity
DragAndDrop.prototype.initialize = function() {
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.throw, this);
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.drag, this);
this.app.mouse.on(pc.EVENT_MOUSEUP, this.drop, this);
console.log('start');
this.on('destroy', function() {
this.app.keyboard.off(pc.EVENT_KEYDOWN, this.throw, this);
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.drag, this);
this.app.mouse.off(pc.EVENT_MOUSEUP, this.drop, this);
}, this);
this.dragging = false;
this.pickedEntity = null;
this.force = new pc.Vec3();
};
DragAndDrop.prototype.drag = function (event) {
if (!this.dragging) {
// create raycast to check if there is an entity to drag
var start = this.entity.camera.screenToWorld(event.x, event.y, this.entity.camera.nearClip);
var end = this.entity.camera.screenToWorld(event.x, event.y, this.pickableDistance);
// check for pickable entity
var result = this.app.systems.rigidbody.raycastFirst(start, end);
if (result && result.entity.tags.has('pickable')) {
console.log("pickable");
// start dragging
this.dragging = true;
// get entity
this.pickedEntity = result.entity;
// get correct position and rotation to drag
var dragPosition = this.app.root.findByName('Ball_Position').getPosition(); // ensure this entity exists
var dragRotation = this.pickedEntity.getRotation();
// change rigidbody type to be able to drag
this.pickedEntity.rigidbody.type = 'static';
this.app.root.findByName('Ball_Collision').enabled = false; // ensure this entity exists
// make entity part of player
this.pickedEntity.reparent(this.entity);
console.log('reparented');
// apply correct position and rotation
this.pickedEntity.setPosition(dragPosition);
this.pickedEntity.setRotation(dragRotation);
}
}
};
DragAndDrop.prototype.drop = function (event) {
if (this.dragging) {
// get correct position and rotation to drop
var dropPosition = this.pickedEntity.getPosition();
var dropRotation = this.pickedEntity.getRotation();
// make entity part of world
this.pickedEntity.reparent(this.app.root.findByName('Root')); // ensure this entity exists
// apply correct position and rotation
this.pickedEntity.setPosition(dropPosition);
this.pickedEntity.setRotation(dropRotation);
// change rigidbody type back to original
this.pickedEntity.rigidbody.type = 'dynamic';
this.app.root.findByName('Ball_Collision').enabled = true; // ensure this entity exists
// end dragging
this.dragging = false;
}
};
DragAndDrop.prototype.throw = function (event) {
if (this.dragging) {
// the key to throw the object
if (event.key === pc.KEY_T) {
// get correct position and rotation to drop
var dropPosition = this.pickedEntity.getPosition();
var dropRotation = this.pickedEntity.getRotation();
// make entity part of world
this.pickedEntity.reparent(this.app.root.findByName('Root')); // ensure this entity exists
// apply correct position and rotation
this.pickedEntity.setPosition(dropPosition);
this.pickedEntity.setRotation(dropRotation);
// change rigidbody type back to original
this.pickedEntity.rigidbody.type = 'dynamic';
this.app.root.findByName('Ball_Collision').enabled = true; // ensure this entity exists
this.force.copy(this.entity.forward).scale(this.throwPower);
this.pickedEntity.rigidbody.applyImpulse(this.force);
// end dragging
this.dragging = false;
}
}
};
If anyone could help I’d be grateful,
Thanks,
Finlay