So, i am working on a 3rd person game where you can do quests and find monsters, etc. Right now im working on having a monster, and having it get hurt when it collides with my sword object. When the health goes below 0, the monster is destroyed. The monster will have a script attached that has all the gamecomponents, like health, magic, strenght, etc. However, i am stumped at where to access the monster’s health and how to lower it. Do i need to make the variables public and change them like that? Or do i need to access components? Any help would be useful.
Heres the monster stats code:
using UnityEngine;
using System.Collections;
public class monsterStats : MonoBehaviour {
private int health = 5;
// Update is called once per frame
void Update () {
if(health <= 0)
Destroy(gameObject);
}
}
Here is my collision source right now…it is attached to the sword as of right now:
using UnityEngine;
using System.Collections;
public class swordCollider : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject == GameObject.FindWithTag("Monster")) DestroyObject(collision.gameObject);
}
}
Lookup SendMessage, it is designed to do exactly what you are after:
using UnityEngine;
using System.Collections;
public class monsterStats : MonoBehaviour {
private int health = 5;
[COLOR=red]
void Attacked(int damage)
{
health -= damage;
}[/COLOR]
// Update is called once per frame
void Update () {
if(health <= 0)
Destroy(gameObject);
}
}
And this…
using UnityEngine;
using System.Collections;
public class swordCollider : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject == GameObject.FindWithTag("Monster"))
{
[COLOR=red] collision.gameObject.SendMessage("Attacked",1);[/COLOR]
}
}
}
Have you considered looking at the Lerpz Tutorial? The ThirdPersonStatus script uses a similar theory where if a monster/enemy touches the player, the player gets hurt. This can work in reverse where a player hurts the monster. I had actually thought of creating an enemy in my own game that takes more than one hit to kill it, but the same theory applies here when working against the player.
var target : Transform;
var damage = 1;
private var lastHitTime = 0.0;
function Start ()
{
if (!target)
target = GameObject.FindWithTag("Player").transform;
}
function OnTriggerEnter(col : Collider){
//Debug.Log("Trigger Used");
if(col.gameObject.tag == "Player")
{
if (MyWalkerController.invincible == false)
{
if (Time.time > lastHitTime + 1.00 )
{
target.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
slamDirection.y = 0;
slamDirection.z = 1;
if (slamDirection.x >= 0)
slamDirection.x = 1;
else
slamDirection.x = -1;
target.SendMessage("Slam", transform.TransformDirection(slamDirection));
lastHitTime = Time.time;
}
}
}
}
Basically this states that if my player gets touched by the enemy, he takes 1 damage, but I can set that damage to 0, 2, 5, even 222 if I wanted. While my ThirdPersonStatus Script keeps track of my life. There’s a little bit of 3rd Person Platformer in there, but it’s the same concept. I’m sending a the message “ApplyDamage” to tell my player that I’m receiving a certain amount depending on what my enemy is set to. If your monster health is somewhat random, and same with the damage, you can still apply the same theory (Sorry that this is Javascript, but C# commands aren’t that much different for the engine)
monsterStats itsScript = monsterGO.GetComponent<monsterStats>();
// then you can do stuff like
itsScript.health -= 10 // public variable in the script that you directly change
itsScript.HitMe(-10); // public function in the script that changes (private) health variable
It’s still pretty quick, but it does have its overheads.
It can be 100s of times slower, especially if you cache references directly to the other script.