var Touch = pc.createScript("touch");
// initialize code called once per entity
Touch.prototype.initialize = function () {
this.pos = new pc.Vec3();
this.cameraEntity = this.app.root.findByName("Camera");
// Only register touch events if the device supports touch
var touch = this.app.touch;
if (touch) {
touch.on(pc.EVENT_TOUCHSTART, this.onTouchStart, this);
}
this.on('destroy', function () {
if (touch) {
touch.off(pc.EVENT_TOUCHSTART, this.onTouchStart, this);
}
}, this);
};
Touch.prototype.updateFromScreen = function (screenPos) {
// Use the camera component's screenToWorld function to convert the
// position of the mouse into a position in 3D space
var depth = 20;
this.cameraEntity.camera.screenToWorld(screenPos.x, screenPos.y, depth, this.pos);
// Finally update the entity's world-space position
this.entity.setPosition(this.pos);
};
Touch.prototype.onTouchStart = function (event) {
// Update the entity's position only when touched
this.updateFromScreen(event.touches[0]);
event.event.preventDefault();
};
In this line of code, there is an object on the screen and I want to drag and drop this object to a different place, but wherever I touch on the screen I touch it, how can I fix it.