Hello,
I am making a quest HUD and I need to make it so that whenever I touch a certain entity it will add 1 to a variable called txt, I will set up a bunch of if statements retrieving txt and changing the text accordingly. I have two problems: 1) I can not figure out how to get when I collide with a certain entity, can I also find out when I am touching a certain rigid body? 2) I can not figure out how to retrieve variables, I have var txt = 1;
to call and if(this.txt = 1){}
.
Any help would be appreciated.
Hi @Codeknight999!
How does your collision code currently look like?
If this.txt
is just a value, you can do something like below.
if (this.txt === 1) {
// your code
}
I am using a modified version of the teleport code from the basic Roll a Ball project, this code will just detect when a player touches a house and change the text when touching the house, otherwise, it will just be blank.
var Housebegger = pc.createScript('housebegger');
Housebegger.attributes.add('target', {
type: 'entity',
title: 'Target Entity',
description: 'The target entity where we are going to teleport'
});
// initialize code called once per entity
Housebegger.prototype.initialize = function() {
};
// update code called every frame
Housebegger.prototype.update = function(dt) {
if (this.target) {
// Subscribe to the triggerenter event of this entity's collision component.
// This will be fired when a rigid body enters this collision volume.
this.entity.element.text = 'Get away, begger!';
}else{
this.entity.element.text = '';
};
};
Thanks for showing me how to extract the variables, that worked
I don’t think your current script can detect collision. Please check the page below to learn more about collision and triggers. You can also find some code examples on that page.
https://developer.playcanvas.com/en/tutorials/collision-and-triggers/
I looked at that, and I got how to work the collision, but how do I make it detect collision with a certain object?
Is there some sort of javascript proximity api.
In the code below two examples.
var Collider = pc.createScript('collider');
// initialize code called once per entity
Collider.prototype.initialize = function () {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
Collider.prototype.onCollisionStart = function (result) {
if (result.other.tags.has('Target')) {
// entity with a specific tag
}
if (result.other.name === 'Player') {
// entity with a specific name
}
};
I am gettng an error:
Cannot read properties of undefined (reading ‘on’)
Any idea how to fix this, also I tried the other code, I did not get an error but it does not work. I used that code and gave the collision entity and the template a tag with Target. Any idea why this is happening?
Mostly you get that error when the entity with the script has no collision component.
I have found a game design error, I was applying the script to the text. I tried adding collision to the text but that did not work. I looked at the playcanvas this.app.fire page, I think if I use collider script and this.app.fire on collision and a text entity that receives it and changes text, that will work. One problem though, I can’t figure out how to use if and this.app.fire is there a way to do this.
Can you share the editor link of your project so I can take a look? Please also let me know which entities and scripts to check.
Sorry for the late reply,
Here is my project: PlayCanvas 3D HTML5 Game Engine
The character is in the hierarchy called Player.
I expect the script to be on for example the ‘Player’ entity, that has a collision component and a rigidbody component.
You need to modify your script a bit, so you are still be able to change the text entity.
var Collider = pc.createScript('collider');
// initialize code called once per entity
Collider.prototype.initialize = function () {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
Collider.prototype.onCollisionStart = function (result) {
if (result.other.tags.has('Target')) {
this.entity.findByName('Text2').element.text = 'Your new text';
}
};
Thanks, that works! Is there a way to make it so it only shows the text when colliding? I was playing around with it and couldn’t find a way.
You probably want to remove or disable the text with the collisionend
event.
https://developer.playcanvas.com/en/tutorials/collision-and-triggers/
It worked! Thanks a lot for your help.