[SOLVED] How to add player pickup distance

Salutation everyone! I need help with modifying some code I found to create a distance to where the player can hold an entity so it doesn’t look like it’s just hovering there.

Hi @Jacob_McBride1,

Try sharing your code in question so people can be of help with your question.

var DragAndDrop = pc.createScript('dragAndDrop');

// initialize code called once per entity
DragAndDrop.prototype.initialize = function() {
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.drag, this);
    this.app.mouse.on(pc.EVENT_MOUSEUP, this.drop, this);

    this.on('destroy', function() {
        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.pickableDistance = 1;

DragAndDrop.prototype.drag = function (event) {
    if (!this.dragging) {
        // create raycast to check if there is an entity to drag
        var screenCenterX = (this.app.graphicsDevice.width / 2) * this.app.graphicsDevice.maxPixelRatio;
        var screenCenterY = (this.app.graphicsDevice.height / 2) * this.app.graphicsDevice.maxPixelRatio;
        var start = this.entity.camera.screenToWorld(screenCenterX, screenCenterY, this.entity.camera.nearClip);
        var end = this.entity.camera.screenToWorld(screenCenterX, screenCenterY, this.pickableDistance);

        // check for pickable entity
        var result = this.app.systems.rigidbody.raycastFirst(start, end);

        if (result && result.entity.tags.has('pickable')) {
            // start dragging
            this.dragging = true;

            // get entity
            this.pickedEntity = result.entity;

            // get correct position and rotation to drag
            var dragPosition = this.pickedEntity.getPosition();
            var dragRotation = this.pickedEntity.getRotation();

            // change rigidbody type to be able to drag
            this.pickedEntity.rigidbody.type = 'kinematic';

            // make entity part of player

            // apply correct position and rotation 

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

        // apply correct position and rotation 

        // change rigidbody type back to original
        this.pickedEntity.rigidbody.type = 'dynamic';

        // end dragging
        this.dragging = false;

Hi @Jacob_McBride1! As far I see that’s the code from one of my example projects, but unfortunately I don’t understand your question.


ah yeah. See there is a distance from where the player can pick up an entity, I wanted to reduce it but I think I found out how. Can you help me incorporate this script system into my other game? Also, it is amazing to read your clean and awesome code. I’d like to learn from you. You’re very good at it.

Yes, I think you already changed the pickable distance from 10 to 1. You can also change the drag position on line 37 and use for example the position of the camera entity itself.

You only need to attach the script to the camera entity of your game.

Thank you very much! :pray:

1 Like

Hmm, it seems as though the ground box entity affects the crosshair entity. I warped the ground to be flatter and less boxy but it inturn misshaped the crosshair. any ideas as to how to fix this issue?

I think I fixed it. I returned to the editor and made the crosshair horizontally squished for when I played it was vertically squished now that I did that it is now circular as the outcome.

If I remember correctly the crosshair is just an UI element, so the ground entity can not effect this.

1 Like

that’s correct. I must have affected the 2d screen while adjusting the shape of the ground. simple mistake.

can you help me with my game in any case?

I have a few things I’d like to add.

You can post your questions on the forum or send me a private message.

1 Like

how do I send you a private message?

You can click on my profile and use the blue message button.

1 Like