Making Entity lose scale after collision (Question)

Ok so i know how to do this but at the same time what i do know wont work for this considering i dont know how to embed the collision in the script so when the enemy collides with the player then he loses scale.X and health goes down on X scale. If someone has a tutorial on collision that could help me i would appreciate it

Literally all you asked for is in this official tutorial: https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

Omg thank you so much man :slight_smile:

@devMidgard Ok so i got this script i just made running but i have a problem with .getLocalScale(); whenever i load the game it says its undefined but it is defined take a look…

var PlayerHealth = pc.createScript('playerHealth');

// initialize code called once per entity
PlayerHealth.prototype.initialize = function() {
    this.health = this.app.root.findByName('pHealth');
    this.takeDamage = 0;
    this.entity.collision.on('collisionStart', this.onCollisionStart, this);
};

// update code called every frame
PlayerHealth.prototype.update = function(dt) {
    var scale = new pc.Vec3().copy(this.healthEntity.getLocalScale());
    
    
    
    if (scale.x < 0.01) {
        scale.x = 0;
        this.entity.script.dead.dead = true;
    }
    if (scale.x > 1) {
        scale.x = 1;
    }
    if (this.takeDamage > 0) {
        scale.x -= this.takeDamage;
        this.takeDamage = 0;
    }
    this.healthEntity.setLocalScale(scale);
};
PlayerHealth.prototype.onCollisionStart = function (result) {
 if (result.other.rigidbody) {
      scale.x = pc.math.lerp(scale.x, 0, 0.5);
 }  
        this.healthEntity.setLocalScale(scale);
};

I had this same problem when i did my enemy health but found i had the script attached to two entitys but that is not the case now now when i look its only connected to just the player entity… should i try getScale? i dont even know if thats a thing :slight_smile: Thanks

Work backwards from the error message.

The error message would probably something along the lines of ‘Cannot call getLocalScale on undefined’

This means that the JS object that you are calling getLocalScale is undefinied. What JS object is this? What could be causing it to be undefined?

Ahh yes i didnt have it in Update :slight_smile: but now it says Cannot read property ‘on’ of undefined this.entity.collision.on('collisionstart', this.onCollisionStart, this); Which i did this off the tutorial in initialize :slight_smile: if i find anything before hand ill let you know

It’s basically the same error message. What JS object is ‘on’ being called on? What could be causing it to be undefined?

1 Like

well i would say its calling on the enemys collision and it is defined isnt it? maybe i should be putting it in update? but the tutorial said it goes in intialize

Is it? What makes it defined?

Well i assume from the tut i looked at i didnt need to define it as a varibale
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
and shows it to ‘Attach an event handler to an event’ how would i define it if it isnt defined? Well i mean like i know how to but is there a specific way to use it?

What is this.entity.collision supposed to point to? Read this post to understand more about how PlayCanvas is architected: How to refer to different parts of the API while scripting

its supposed call collision from the enemy saying if the player and the enemy collides the player loses health… i just realized that it was calling undefined because the player health bar didnt have a collision.
But now the health isnt going down :frowning: ill keep working and let you know what happens

It’s referring to the collision component of the current entity that the script is attached to.

oh so for it to take health when it collides with the playe i would have to attach it to the enemy?
Also scale is calling undefined

       var scale = new pc.Vec3().copy(this.health.getLocalScale());
if (result.other.rigidbody) {
     scale.x = pc.math.lerp(scale.x, 0, 0.5);
}  
       this.healthEntity.setLocalScale(scale);
};```

Actually @yaustar instead of collision why not trigger?

Because there isn’t a trigger component in PlayCanvas.


this tutorial is called Collisions and Triggers… could i not make it this same way?
could this not work the same way as a collider?

‘trigger’ is the name of the script in the tutorial that uses the ‘collision’ component from the PlayCanvas engine. The two are not interchangable.

OH ok well im going to try something then i will let you know if i get it right thanks!

ok @yaustar this is what i have come up with so far but still no response

var PlayerHealth = pc.createScript('playerHealth');

// initialize code called once per entity
PlayerHealth.prototype.initialize = function() {
    this.entity = this.app.root.findByName('pHealth');
    this.entity.collision.on('collisionstart', this.onHit, this);
};

PlayerHealth.prototype.onHit = function(result) {
    var scale = new pc.Vec3().copy(this.entity.getLocalScale());
    scale.x = pc.math.lerp(scale.x, 0, 0.5);  // SCALE DOWN
    this.entity.setLocalScale(scale);
};