[SOLVED] Collision pick up

I created a new script called dropable and deleted the collision and replaced it with the mouse event
however when I pasted in the coding for the actual entity to drop to the root file it did not work and event in the code editor was crossed off

Yes.

Below (outside) your onCollisionStart function.

I think it’s better to add it to the pickable script, but if you create a new script be sure everything in the script has the new script name.

var Pickable = pc.createScript('pickable');

// initialize code called once per entity
Pickable.prototype.initialize = function () {
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.pickedUp = false;
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
Pickable.prototype.onMouseDown = function (event) {
    if (event.button === pc.MOUSEBUTTON_RIGHT) {  
        this.entity.reparent(this.app.root.findByName('Root'));
        this.pickedUp = false;
    }
};

Pickable.prototype.onCollisionStart = function (result) {
    if (result.other.name === 'Player' && !this.pickedUp) {
        this.pickedUp = true;
        var point = result.other.findByName('Point');
        this.entity.reparent(point);
        this.entity.setPosition(point.getPosition());
    }
}

is this correct?

No, this part is inside the initialize function right now, that’s not correct.

I need this to be writen out please I cant put them in the right spot I have already tried so many tutorials

This is the whole onMouseDown function. There is already a onCollisionStart function in your script. Just copy and past this onMouseDown function below the other onCollisionStart function.


// initialize code called once per entity
Pickable.prototype.initialize = function () {
this.pickedUp = false;
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
Pickable.prototype.onMouseDown = function (event) {
    if (event.button === pc.MOUSEBUTTON_RIGHT) {  
        this.entity.reparent(this.app.root.findByName('Root'));
        this.pickedUp = false;
    }
};
};

Pickable.prototype.onCollisionStart = function (result) {
    if (result.other.name === 'Player' && !this.pickedUp) {
        this.pickedUp = true;
        var point = result.other.findByName('Point');
        this.entity.reparent(point);
        this.entity.setPosition(point.getPosition());
    }

};

if this does not work does the script go under the pickable.prototype.oncollisionstart = function (result)

Why you don’t try to do, what I explain? First of all, remove the onMouseDown from the initialize function.

done back with the normal pickable script

I give up. Here you go:

var Pickable = pc.createScript('pickable');

// initialize code called once per entity
Pickable.prototype.initialize = function () {
    this.pickedUp = false;
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
};

Pickable.prototype.onCollisionStart = function (result) {
    if (result.other.name === 'Player' && !this.pickedUp) {
        this.pickedUp = true;
        var point = result.other.findByName('Point');
        this.entity.reparent(point);
        this.entity.setPosition(point.getPosition());
    }
};

Pickable.prototype.onMouseDown = function (event) {
    if (event.button === pc.MOUSEBUTTON_RIGHT) {  
        this.entity.reparent(this.app.root.findByName('Root'));
        this.pickedUp = false;
    }
};

I am still a beginner.
sorry for wasting your time.

But still, your a great programmer I could have never gotten this far without you.

Thank You

I understand you are a beginner, that’s why I explain every step, you just have to follow the steps. If I do everything for you will not learn from it.

If something doesn’t work or isn’t right, restore it to the old situation, otherwise everything will be a mess.

Ok thank you.

Does it work right now?

Yes it did
it did not work with root as the file
I created a new entity and made it go there and now the script works perfectly.

This should work too, but I’m happy it works for you now.

Thanks.