[SOLVED] Collision pick up

When the player is touching an entity, I that entity to go to the player camera entity so the entity will follow were the player moves and looks
can someone please help me with this script

You can try to reparent the entity in a collision function.

https://developer.playcanvas.com/api/pc.Entity.html#reparent

Example:

Collider.prototype.onCollisionStart = function (result) {
    if (result.other.name === 'Player') {
        this.entity.reparent(result.other);
    }
};

Note that your collision component is probably not in front of the camera.
That means that you can’t see the entity when it is picked up.

Will this make it so it will go to the camera in the player so I can see it when in the camera entity.

I suggest to add a empty entity as a child of your camera (with for this example the name ‘Point’) and place this entity in front of your camera. Then you can do something like below.

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

Would this be under the initialize or the update function?

No, maybe create a new script with the name pickable and add this script to the entity. Replace the code with the code below. Make sure all the names in the script are the same as the names in your scene.

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);
};

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());
    }
};

the entity I want to pick up right
or the new entity I added to the camera?
which one would I add the script to?

Yes, this entity need the script.

the entity has the name point and it is in front of the camera.
Is that what I add the script to?
sorry for all the questions I am still a beginner with scripting
I am used to block coding.

oh the entity I want to pick up ok

No, the entity that you want to pick up need the script, a collision component and a rigidbody component.

static type rigidbody

No, kinematic.

ok I will try it to see if it goes to the camera

It worked but it flung the player out of the world

Make sure the collision component doesn’t make contact with the player’s collision component after picking up.

so using a mesh collision depending on the object size might result in flinging the player

I suggest never using a mesh collision. Eventually you can disable the collision component after picking up the entity.

ok

I went through the floor