Just a script that automatically sets an entity’s collision.
Has support for box, capsule, plane, sphere, and cylinder.
BE SURE TO READ THE COMMENTS IN THE SCRIPT BEFORE USE
Just create a script and copy-paste this in there:
var AutoCollision = pc.createScript('autoCollision');
// initialize code called once per entity
AutoCollision.prototype.initialize = function() {
this.scaleSize = new pc.Vec3()
};
// update code called every frame
AutoCollision.prototype.update = function(dt) {
this.scaleSize = this.entity.getLocalScale()
//box
if (this.entity.render.type == "box"){
this.collisionSize = new pc.Vec3(this.scaleSize.x/2,this.scaleSize.y/2, this.scaleSize.z/2)
this.entity.collision.halfExtents = this.collisionSize
}
//capsule
//X scale and z scale must be equal
if (this.entity.render.type == "capsule"){
this.collisionSize = new pc.Vec2(this.scaleSize.x/2,this.scaleSize.y * 2)
this.entity.collision.radius = this.collisionSize.x
this.entity.collision.height = this.collisionSize.y
}
//cylinder
//X scale and z scale must be equal
if (this.entity.render.type == "cylinder"){
this.collisionSize = new pc.Vec2(this.scaleSize.x/2,this.scaleSize.y)
this.entity.collision.radius = this.collisionSize.x
this.entity.collision.height = this.collisionSize.y
}
//sphere
//Everything must be equal
if (this.entity.render.type == "sphere"){
this.collisionSize = new pc.Vec2(this.scaleSize.x/2,this.scaleSize.y)
this.entity.collision.radius = this.collisionSize.y
}
//plane
//Everything must be equal
if (this.entity.render.type == "plane"){
this.collisionSize = new pc.Vec3(this.scaleSize.x/2,0, this.scaleSize.z/2)
this.entity.collision.halfExtents = this.collisionSize
}
};
// swap method called for script hot-reloading
// inherit your script state here
// AutoCollision.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/