[SOLVED ]If i did a collision to pick up a weapon

Quick question could i use a box/cube to use the collision to make my character pick up the weapon? I will edit anymore questions and detail when i learn more about it
EDIT: Well i kinda went through and i came up with this:

var Weapon = pc.createScript('weapon');

// initialize code called once per entity
Weapon.prototype.initialize = function() {
    this.terrain = this.app.root.findByName('terrain');
    this.safe = true; //Safe is used to determin if the weapon will kill someone or not.
            this.owner = "";
            
            this.entity.collision.on("collisionstart", this.onCollisionStart, this);
};

// update code called every frame
Weapon.prototype.update = function(dt) {
    if(this.entity.getPosition().z !== 0.5 && this.entity.getParent() == "Weapons"){
                this.entity.setPosition(0, 5, 0.5);
                console.log(this.entity.getPosition());
            }
};
Weapon.prototype.onCollisionStart = function(result) {
  if(result.other.getName() === "terrain") {
                this.safe = true;
                this.owner = "";
            }  
};

But still the weapon does not attach to the entity

1 Like

Try making the weapon a child entity of the character upon collision. That should work.

When you say child entity you mean put it on the character as like a folder? like the camera is a child of the Player? @DevilZ

Exactly. That’s precisely what I meant.

But when i do that the weapon just follows the player entity everywhere… how can i fix this?

Make it a child only upon collision.

Ps, would you like help with this game? I can help out! We can publish under my Game Studio, Renegade Lab Studios. We could also publish it to an upcoming game jam!

1 Like

Ok! i would love that thanks for your help also! :slight_smile:

btw im nathan_hawkins without the 1

btw @DevilZ btw would it like this when i make it a child of the player on collision?:

      this.Player.addChild("Weapons");

here is a longer piece of code:

Weapon.prototype.onCollisionStart = function(result) {
  if(result.other.getName() === "Player") {
                this.safe = true;
                this.owner = "";
      this.Player.addChild("Weapons");
            }  
};

This would work @Nathan_Hawkins1

How can we connect? Are you on Skype?

No i dont have a working phone or skype right now i got to get my screen replaced this is my only solution right now:
Ok so when i did that it said that the graphnode was already parented:
30%20AM%20-%20Edited
could this be why?

Probably. Fixing this should solve it.

Nope still didnt fix it :slight_smile: sorry it says its already parented but its not :thinking:

Root is the parent in your case.

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

1 Like

So if i was to assign a new parent i would have to removeChild likeso:

            this.app.root.removeChild("AK47");

but how would i reparent?


i do not completely understand how i am supposed to use this @yaustar would i pc.GraphNode.reParent
EDIT: Ok so i seen what i was supposed to do:

this.weaponEntity.reparent(¨Player¨);

But now calls an error saying parent.addChild is not a function when i havent even put anything of the sort in my code:

var Weapon = pc.createScript('weapon');

// initialize code called once per entity
Weapon.prototype.initialize = function() {
    this.Player = this.app.root.findByName("Player");
    this.weaponEntity = this.app.root.findByName('AK47');
    this.terrain = this.app.root.findByName('terrain');
    this.safe = true; //Safe is used to determin if the weapon will kill someone or not.
            this.owner = "";
            this.entity.collision.on("collisionstart", this.onCollisionStart, this);
};

// update code called every frame
Weapon.prototype.update = function(dt) {
    if(this.entity.getPosition().z !== 0.5 && this.entity.getParent() == "AK47"){
                this.entity.setPosition(0, 5, 0.5);
                console.log(this.entity.getPosition());
            }
};
Weapon.prototype.onCollisionStart = function(result) {
  if(result.other.getName() === "Player") {
                this.safe = true;
                this.owner = "";
      this.Player.reparent("AK47");
            }  
};

Looking at the documentation, what object type is reparent expecting to be passed to it?

Well @yaustar i am trying to remove the parent from ¨Root¨ and make the parent the ¨Player¨ onCollision so when the player collides the player is now the parent of the weapon. Am i making sense?
EDIT: Here is project in case you want to look

I understand that’s what you want to do, I’m asking if you understand what the documentation is saying how the function should be used.

Again, looking at the documentation, what object type is reparent expecting to be passed to it?