Ok so i have been trying to get my entitys health to go down for about a week now and now i kind of had an idea… So everything inside the editor is usable inside the code editor right? so if i take the health entity scale and when the entity is damaged the scale of the health bar will go down
HERE IS MY SLIGHT IDEA:
Thanks @Pieter ! i took a look here is what i came up with so far out of what i have learned but not sure where to go next take a look if you want : https://playcanvas.com/editor/scene/722434
Here is what i have in my code
Doesn’t do anything yet but what can i do for the code update? Also this isnt really done im still working on it so some of it doesnt even have its right name
Your script is attached to enemy - which means this.entity refers to your actual enemy not the health one.
First get the proper health entity -> this should scale your health to half its size.
Health.prototype.initialize = function() {
this.healthEntity = this.entity.findByName('eHealth'); // we're searching inside the entity
var scale = new pc.Vec3().copy(this.healthEntity.getLocalScale()); // store the scale inside a new vec3 (scale is a vec3)
scale.x *= 0.5; // scale x to half its size
this.healthEntity.setLocalScale(scale); // set the scale
};
I havent tested this - but keep in mind that you cant check for mousepresses or buttons in ‘initialize’ - this is only run once when the entity becomes active.
If you want to change it dynamically put your code in .update.
first test this and check if the health entity actually becomes half its size.
-Pieter
So if i change the enemy to dynamic when i get the movement then i would have to call the health to half size?
also it works but how would i put this in so it only goes half health when the weapon hits the entity here is my call in -Update- function
Health.prototype.update = function(dt) {
if (this.app.keyboard.isPressed(pc.MOUSEBUTTON_LEFT)) {
this.healthEntity.setLocalScale(scale);
};
};
i am not sure if this is how i would do this but ur code works.
also if i am to use another entity to reduce the size of the healthEntity would i have to use the same instance as you did with the health?
EDIT:
ALSO also what would be a text element constructor? i tryed this at first:
var text = new pc.text_string.copy(this.textEntity.getLocalTransform());
text -= 50;
but when i realized i was doing so constructor incorrectly i switched it to this:
var text = new pc.ELEMENTTYPE_TEXT.copy(this.textEntity.getLocalTransform());
text -= 50;
but still does not work as a constructor… if you know a element constructor for text let me know ty
No - the dynamic you are referring to is related to physics - that is something completely different. I meant if you want to change the scale during gameplay/runtime you have to put code in update.
Your update example looks fine but you have to change the scale value each frame if you want to see it change. e.g.
Health.prototype.update = function(dt) {
if (this.app.keyboard.isPressed(pc.MOUSEBUTTON_LEFT)) {
scale.x *= 0.99; // this will scale down the x value of scale.
this.healthEntity.setLocalScale(scale);
};
};
yes i am kind of testing random things out but what i can see from the the element this is where i have been looking at and i assumed that since pc.Vec3 worked to make a change in scale i could use pc.ELEMENTTYPE_TEXT in the same manor but also notifies me as not being a constructor so i have been looking and i seen when i highlight the text element it shows this:
so as i looked at this i thought maybe this would work as a constructor but i was wrong i am still looking at any possible alternative route to work this out but my question is woul i be able to do this?
var text = new pc.string('nHealth').copy(this.textEntity.getLocal...);
If you have anymore suggestions or comments on this i would be grateful @Pieter
EDIT:
Ok i have got the healthEntity going down thanks to @yaustar telling me to use app.mouse but now here is my code in -Update- function:
var scale = new pc.Vec3().copy(this.healthEntity.getLocalScale()); // store the scale inside a new vec3 (scale is a vec3)
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
scale.x *= 0.50;
this.healthEntity.setLocalScale(scale);
} else if (this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT)) {
scale.x ++;
}
But now when i release the mouse button the health scale does not go up? am i using the correct operators? or do i have to use the opposite of this *= division? which i dont see why that would work but could i not do this:
else if (this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT)) {
scale.x *= 0.99;
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
scale.x *= 0.50;
this.healthEntity.setLocalScale(scale);
} else if (this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT)) {
scale.x ++;
// You also need to do something with the updated scale value
// Add this line below
this.healthEntity.setLocalScale(scale);
}
But now that means the entity will just expand to much instead of just to where i want it to go which is its original form
so whats supposed to happen is when the entity is shrunk then it grows back slowly until it hits its original scale again
But now when i release the mouse button the health scale does not go up?
With the code above, releasing the mouse button now scale the health up. Now you need to work on the next step ( having suitable values ). A lot of coding when you’re learning is doing a step at a time.
Search for the lerp functionality, it should allow you to scale down and up to relevant values.
Try changing the code, so that when shrinking down scale.x, use this…
scale.x = pc.math.lerp(scale.x, 0, 0.5);
When scaling up, use this…
scale.x = pc.math.lerp(scale.x, 1, 0.5);
Remove all other locations where you modify scale.x, and the two lines above will scale that value down to 0, and up to 1, using 0.5 as the “alpha”. Change 0.5 to speed up / slow down the rate at which the values change ( not time dependant for now, but you can change that later )
after i clicked the mouse button down and released it would move back to this scale?
and i would have to edit this around to make it move up just a little bit after like say a second go up from
(0.2, 0.2, 0)
then after a few seconds of not being hit it would go up
(0.5, 0.2, 0)
Like so? or is there a way to make a statement before this code is called that says wait a few seconds before calling on lerp?
Try this ( taking it from the original code ), to see if the UI element scales down and up. From there, play around with the 3rd value ( currently 0.5 ) for speed ( lower value is slower ).
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
scale.x = pc.math.lerp(scale.x, 0, 0.5); // SCALE DOWN
this.healthEntity.setLocalScale(scale);
} else if (this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT)) {
scale.x = pc.math.lerp(scale.x, 1, 0.5); // SCALE UP
this.healthEntity.setLocalScale(scale);
}
Ok yes that works and thats actually good… but when you click again the health goes up again which i dont want (Ill get into more of destroying entity later)
But when i release the mouse i want the health to regen to its original scale.
I will let you know if i find anything out @Mal_Duffin
Yes thank you so much man i appreciate all of your guys help i have learned so much and hope to make an incredible game of my own Quick question do you think it is ok just to link the game to my own website?
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
scale.x = pc.math.lerp(scale.x, 0, 0.5); // SCALE DOWN
this.healthEntity.setLocalScale(scale);
} else if (this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT)) {
scale.x = pc.math.lerp(scale.x, 1, 0.5); // SCALE UP
this.healthEntity.setLocalScale(scale);
}
If the mouse button is held down for that frame, then the scale moves towards zero.
If not, then for the one single frame that the mouse was released after being held down, it moves toward 1 for that frame.
I think you probably don’t want that check for the mouse was released, and you’ll get the entity shrinking when you hold it down, and moving back up when you release it.
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
scale.x = pc.math.lerp(scale.x, 0, 0.5); // SCALE DOWN
} else {
scale.x = pc.math.lerp(scale.x, 1, 0.5); // SCALE UP
}
this.healthEntity.setLocalScale(scale); // SET THE ENTITY TO HAVE THAT SCALE
If you don’t want the scale to go back up, then remove the // SCALE UP line. If you want it to return to full faster, then change 0.5 to a value closer to 1. If you want it to return to full, then set scale.x to 1.