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


#1

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


#2

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


#3

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? @coderMACi5


#4

Exactly. That’s precisely what I meant.


#5

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


#6

Make it a child only upon collision.


#7

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!


#8

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


#9

btw im nathan_hawkins without the 1


#10

btw @coderMACi5 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");
            }  
};

#11

This would work @Nathan_Hawkins1


#12

How can we connect? Are you on Skype?


#13

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?


#14

Probably. Fixing this should solve it.


#15

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


#16

Root is the parent in your case.

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


#17

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

#18

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


#19

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


#20

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?