- Home /
Changing Mesh of an object depending on Health Value. C#
I'm trying to set up code that will cause the mesh of the player to change depending on how much health they have. I believe that I'm supposed to use MeshFilter.mesh but I'm not completely sure how to do so.
Code I'm using:
public class PlayerHealth_Test : MonoBehaviour {
public float CurrentHealth = 100.0f;
public GameObject Player;
private float MaxHealth = 100.0f;
public bool MeshChange = false;
public Mesh NoDam;
public Mesh LowDam;
public Mesh MedDam;
public Mesh HighDam;
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
void Update () {
if (CurrentHealth > 100)
{
CurrentHealth = MaxHealth;
}
if (MeshChange == true);
{
if (CurrentHealth == 100.0f)
{
Mesh = GetComponent<MeshFilter>(NoDam).mesh;
}
if (CurrentHealth <= 75.0f)
{
Mesh = GetComponent<MeshFilter>(LowDam).mesh;
}
if (CurrentHealth <= 50.0f)
{
Mesh = GetComponent<MeshFilter>(MedDam).mesh;
}
if (CurrentHealth <= 25.0f)
{
Mesh = GetComponent<MeshFilter>(HighDam).mesh;
}
}
if (CurrentHealth <= 0)
{
GameObject.DestroyObject(Player);
}
}
}
The only errors right now are in the mesh assigning lines of the if statements because, again, I don't really know how to use MeshFilter.mesh. I've looked through the script reference but looking over the example code doesn't really give me a really clear idea on how to use it.
Answer by rutter · Nov 05, 2013 at 07:36 PM
Sometimes it's easier to switch GameObjects altogether, as described here.
Past that, you're probably looking for something more like this:
if (MeshChange) {
Mesh myMesh;
if (CurrentHealth > 75f) {
myMesh = NoDam;
} else if (CurrentHealth > 50f) {
myMesh = LowDam;
} else if (CurrentHealth > 25f) {
myMesh = MedDam;
} else {
myMesh = HighDam;
}
GetComponent<MeshFilter>().mesh = myMesh
}
Keep careful track of your data flow: if you're picking one Mesh from a set, and then assigning it to a MeshFilter, your code should read like you're doing just that.
Watch your function calls: there's no version of GetComponent()
that takes arguments past the type requested.
Thanks for the help, that seems to have worked. I'll need to wait to meet up with the people I'm working with to double check though.
Also, thanks for the advice, I'm rather new to coding so I'm surviving on what I can find on the script reference and youtube so most the really technical (exactly how GetComponent() works/what it's limits are for example) stuff just goes over my head right now. Also, I'll remember that GameObject switching just in case.
There's a steep learning curve in most areas of game dev, but you're asking smart questions at least! Best of luck.
Answer by CodeKenny · Nov 05, 2013 at 07:24 PM
In this case I would probably create 4 textures (or MORE) to represent the health left and swap those ( GUI elements ) according the damage / healing received by the player. And if it looks too bad, make more textures and change them with higher frequency.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Public Health Var C# 1 Answer
Distribute terrain in zones 3 Answers
Enable/Disable Touch during runtime. 0 Answers