How to instantiate a cube to my player

Hi guys. Im new here and new to js.
In my project i have a player who collide with a box and take the box its child.
Other problem i have is . I have movement script and i cant add collison script same entity

Hi @l0rdyolo and welcome!

This is your first problem? You can reparent and entity to make it a child of your player entity.

otherEntity.reparent(playerEntity);

Why you can’t? Can you explain what you try to achieve?

In my scenario, I want my player to move forward and when he hits the boxes that appear in front of him, he eats those boxes and grows up like a snake.

This is my collision script

var Collision = pc.createScript('collider');


// initialize code called once per entity
Collision.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

Collision.prototype.onCollisionStart = function (result) {
    if (result.other.rigidbody) {
        this.entity.destroy();
    }
};

and this is my movement script

var Movement = pc.createScript('movement');

// initialize code called once per entity
Movement.prototype.initialize = function() {
};

// update code called every frame
Movement.prototype.update = function(dt) {
    this.entity.rigidbody.applyForce(0,0,-10);
     if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
        this.entity.rigidbody.applyForce(-5,0,0);
    }
    if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
        this.entity.rigidbody.applyForce(5,0,0);
    }
};

when im adding two script to my player my player cant move

Yes, you can reparent the box and set the local position.

Why you want to add that script to your player entity? In the script you destroy this.entity after collision, which is the player in that case.

Yes i know my player will be destroy if i write this code but i will change it when i learn how to

I realy dont know how to code it

Can you try to replace line 11 of the collision script above, with the line below?

result.other.reparent(this.entity);

Even better is to add the tag pickable to your box entity and then replace line 10 till 12 of the collision script above, with the code below.

if (result.other.tags.has('pickable')) {
    result.other.reparent(this.entity);
}


my player is moving in the direction of the arrow

when it hits the first box it takes it under it and continues to move in the direction of the arrow

When it hits the second box, the second box is placed under the first box and continues to move in the direction of the arrow.

This is scenario…

var Collision = pc.createScript('collider');

Collision.attributes.add('player', {type:'entity'});

// initialize code called once per entity
Collision.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

Collision.prototype.onCollisionStart = function (result) {
    if (result.other.tags.has('pickable')) {
        result.other.reparent(this.entity);
        this.entity.setLocalPosition(this.player.getLocalPosition());
    }
};

i made a tag ‘pickable’ to player and added my code

The player should not have the tag, but the pickable boxes. Then you can add the collision script to your player.

I made it what u told. When player hit the box, box getting random position and player cant move cuz box ghost stay there and blocking my player when i move my player just a bit right or left i mean dodge the ghost box player can contiune moving

I think you need to set the rigidbody component of the boxes to kinematic. Probably it’s static or dynamic right now.

It will conflict with your player collision anyway if it’s on the same position. You have to change the local position of the box after reparenting. For example something like below, but I don’t know the right axis.

result.other.setLocalPosition(0, 0, 2);

Thats worked thank you !

Why i cant change parent localposition in collision script ?

Not sure what you mean. Which entity is the parent of that script you think? I guess that entity is not related.

i mean when we hit the box we are making him a child of our player right ?
That way our parent is player, so im asking why i cant change player position when we hit box

That’s right, but I’m not sure why you want to change the position of the player.

If you want to change the position of the player and your player has a dynamic rigidbody, you can’t set the position of the entity directly. If that’s the case you need to teleport the rigidbody of it. Please check the page below.

https://developer.playcanvas.com/en/tutorials/Using-forces-on-rigid-bodies/

1 Like