[SOLVED] How to add Collider Component to Prefab node?

Hi,

I know the Collider Component is new but I was wondering what the method is to add a collider to a prefab? If the prefab node is moving, how do you make the collider box move with the prefab?

Would be great if the Prefab section of the Quick Start tutorial was updated to include the new Collider Component System. It would help clear things up.

@slackmoehrle and @pandamicro any clue? I mainly would like to know how to move the collider component with the prefab. Ex script:

var newObstacle = cc.instantiate(this.smallRockPrefab);

// Set move action
var newObstacleAction = new cc.MoveBy(this.roadSpeed, cc.p(-this.roadWidth, 0));
newObstacle.runAction(newObstacleAction);

// Add this obstacle prefab to the obstacles Node
this.node.addChild(newObstacle);

I have added a Collider Component to this.node but when I turn on the debug box, the square collider does not move with the rock prefab.

I don’t know off hand. @pandamicro @nantas2

1 Like

I solved it. The solution is to create an empty node and add a collider component then add the prefab as a child to that node and apply the move action to the node rather than the prefab, which is not a child of the node.

properties: {
obstaclePrefab: {
        default: null,
        type: cc.Prefab
    },
obstacleNode: {
        default: null,
        type: cc.Node
    }
}
...
var newObstacle = cc.instantiate(this.obstaclePrefab);

// Set move action
var newObstacleAction = new cc.MoveBy(this.roadSpeed, cc.p(-this.roadWidth, 0));

// Add this obstacle prefab to the obstacles Node
this.obstacleNode.addChild(newObstacle);
this.obstacleNode.runAction(newObstacleAction);

Hi @ efares, @slackmoehrle @pandamicro

Wait a minute, I’m not quite understanding this Prefab / Collider thing… Ok, so first I have a Node, with a Box Collider attached to it, I gave it a Tag #, set up in the Project Settings the Collision Groups, then apply the group name ( drop down ) to the Node . Afterwards, I convert the Node into a Prefab, using CC.NodePool to instantiate / spawn Prefab, and I’m trying for a Prefab ( enemy ) to Collide with my player (Node ), I’ve tried the Bounding Box, Tag Method and still nothing…And now I see that one has to add an additional Node? Isn’t this double work?

Folks, I understand you are very busy updating Cocos Creator with features, but please…por favor…make this collision process much easier to implement, is it possible that within the Collider Components (ex: Box Collider), you can have like a drop down, where a user can choose ( ex: friction, etc…) and then choose which physics engine you want to use from within the Project Settings…?

Currently, I’m already 2 days trying to figure out this arroz con mango collision thing and still nothing…I would really appreciate any help you can offer…Again, thanks for your dedication and hard work in giving the community a promising game engine…God Bless…

Sincerely,

Sunday