Add a collider to a part of the model

Hello. My game has spikes in the floor. they come out of the floor and can kill the character. I move the part of the model that I find in the hierarchy. But it doesn’t have a solid body and a collider. How do I capture a collision with a character correctly? Here is a sample code.

SpykeController.prototype.upSpyke = function(){
    const pos = this.elem.getPosition()
    const y = pc.math.lerp(this.middleValue, this.endPos, 0.3)
    const v = new pc.Vec3(0,y,0)
    this.middleValue = y
    this.elem.setLocalPosition(v)
    if(v.y > 2.9){
        this.isActive = true
        this.middleValue = 3
    }
}

SpykeController.prototype.downSpyke = function(){
    const pos = this.elem.getPosition()
    const y = pc.math.lerp(this.middleValue, this.startPos, 0.009)
    const v = new pc.Vec3(0,y,0)
    this.middleValue = y
    this.elem.setLocalPosition(0,y,0)
    // console.log(this.entity.rigidbody)
        if(v.y < -1.9){
        this.isActive = false
        this.middleValue = -2
        this.timer = 0
    }
}

If you add a collision component to an entity, without adding a rigidbody component, it will become a trigger. A trigger can sense when another rigidbody touches it. It can also be set as kinematic and moved around. You can find more details on triggers here:
https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

As such, you can add a trigger entity that would represent the spikes. It doesn’t have to match the mesh exactly. You can use a cylinder or a box to roughly occupy the same space the spikes do. You would then manually set the position of the trigger to match the visual mesh position and listen on trigger enter events.

2 Likes