Would it make more sense to use collision objects and if facing the right direction and if objects are within each other, then modify health? I’m also confused about the part of the code where it’s making the enemy point toward the player - it seems like the ‘enemies’ in the screenshots are some kind of obstacle, not like something chasing it? If you want to just make health go down as it enters those blue blocks, here’s what I would do:
Make colliders for each. Oh also, I use javascript so please bear with.
function onTriggerEnter(collided : Collider) {
if (collided.name == 'blueBlock') {
health -= collided.gameObject.GetComponent(whateverScript).damage;
// And then maybe some of that other timing cool down stuff here if needed
}
}
My best guess after some quick searching - c# people, please lmk if I’m wrong.
Script on the player:
using UnityEngine;
using System.Collections;
public class playerStuff : MonoBehaviour {
float health = 100.0;
void OnTriggerStay(Collider collided) {
// Not sure if I can do -= in c#
health = SubtractDamage(health);
}
float SubtractDamage(float val) {
float newVal;
// Perhaps here call a function on the enemy to get the amount of damage
newVal = val - 1.0;
return newVal;
}
}
Edit: I keep switching between OnTiggerStay and OnCollisionStay - I don’t fully understand the difference yet. I use Trigger.
using UnityEngine;
using System.Collections;
public class playerStuff : MonoBehaviour {
float health = 100.0;
void OnTriggerStay(Collider collided) {
// Not sure if I can do -= in c#
health = SubtractDamage();
}
float SubtractDamage(float val) {
float newVal;
// Perhaps here call a function on the enemy to get the amount of damage
newVal = val - 1.0;
return newVal;
}
}
I made a separate subtraction function because an example for multiplying I saw did the same. I assume I can’t do +=, -=, etc, but is it not possible to just say val = val - 1.0 in c#? Again, not in an environment where I can test and I don’t use c# just curious.
Did passing health into the function as above help? As for the other error, perhaps try changing all the floats to double and see what happens. I honestly am confused as to where the double is coming in at all, but it’s incompatible with the float. Maybe the 1.0 is the double. Not sure how to make that a float if that’s the case. Maybe see what happens if you keep the floats and just subtract 1 as an integer.
Nah, you are more familiar with c# let’s try to fix it there. Don’t give up!
The second error you are getting is with assigning/modifying floats with constants. 100.0 and 1.0 are doubles by default. To make them floats, add the suffix ‘f’ to the number like so: 100.0f Here is the revised code - still not tested, but hopefully without those errors.
using UnityEngine;
using System.Collections;
public class playerStuff : MonoBehaviour {
float health = 100.0f;
void OnTriggerStay(Collider collided) {
// Not sure if I can do -= in c#
health = SubtractDamage(health);
}
float SubtractDamage(float val) {
float newVal;
// Perhaps here call a function on the enemy to get the amount of damage
newVal = val - 1.0f;
return newVal;
}
}
Also note this is going to take health every frame. You’ll want to make it time based or you may get different health drain on differently performing machines.
Sure - as long as it’s in the same class. Note that you’ll also need to make sure maxHealth is declared in the class. Also, are you able to concatenate strings and floats in c#? If not, use .ToString() to turn those floats into strings on printing.
Also, for debugging I rarely put it to the gui. I often print statements or just look at the value in the inspector. I don’t have to worry about public and private in javascript if I don’t implicitly set it (all my variables show in the inspector) but I think I may have read that only public c# variables will show up in the inspector. Again, not sure about that.