My first problem is getting this enemy to react. I’m sure I’ve gotta be doing something simple wrong. The if statement in the oncollision2d method is never being called. The information in that if statement isn’t being printed.
The name of the asset that collides with the enemy is AttackSpellPrefab. There’s a separate attack script that handles instantiating the attack and sending it along a path. in the hierarchy, when the attack instantiates, an item called AttackSpellPrefab(Clone) is created. Is that the correct name to reference in the above mentioned if statement? that script is attached to the player. the attack spell prefab is then dragged into a slot for a rigidbody2d on the player.
If it makes a difference on where I need to write the script, I would like to be able to switch between different attacks, so I think I would need to create a “spellbook” object that holds the master list of abilities (or just two to get the code going) and reference it from there, with the game manager script checking to see which spells the player has access to. when the player switches attacks, an object (the spellbook object?) should assign the correct image and rigidbody and collider to the player.
I can provide all of my code if you need more than just this class’s code to help.
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour {
Rigidbody2D myBody;
[SerializeField]
float hitPoints = 10; //tweakable number of hit points to initialize with
float currentHitPoints; //HP number to work with in script
Transform myTransform;
//[SerializeField]
// Sprite splatSprite; //sprite to indicate death
// [SerializeField]
//Sprite liveSprite; //sprite to indicate not dead.
SpriteRenderer mySprite;
// Use this for initialization
void Start() {
myBody = GetComponent<Rigidbody2D>();
currentHitPoints = hitPoints;
}
public void OnCollisionEnter2d(Collision2D other)
{
if (other.transform.name == "AttackSpellPrefab(Clone)")
{
currentHitPoints--;
FlashIt();
print("I've been hit!");
print(currentHitPoints);
}
}
IEnumerator FlashIt()
{
GetComponent<SpriteRenderer>().enabled = false;
yield return new WaitForSeconds(.5f);
GetComponent<SpriteRenderer>().enabled = true;
}
public void HealthCheck()
{
if (currentHitPoints < 1)
{
// mySprite.sprite = splatsprite
print("I Die");
}
}
// Update is called once per frame
void Update () {
HealthCheck();
}
}