Hi I am trying to create a game where there are different types of zombies. I decided to create a base class for the zombies that includes methods for moving and flipping characters and then I have individual classes for each type of zombie where I will modify things like health and damage.
I made it so the zombie prefab I put in the game has a script that inherits from the Zombie class but when I run the game nothing in the Zombie.cs script is run.
Anyway, here are my classes:
Zombie.cs
public class Zombie : MonoBehaviour {
//zombie goes after this target
Transform targetPlayer;
//Zombie Rigid Body
Rigidbody2D zombieRB;
//The speed the zombie will move
public float moveSpeed;
//Used for flipping zombie based on which way it is going
bool facingRight = true;
// Use this for initialization
void Start () {
zombieRB = GetComponent <Rigidbody2D> ();
//This finds the player object to follow it
targetPlayer = GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update () {
//Checks zombies position in comparison to player to see where it should move.
if (transform.position.x < targetPlayer.position.x) {
//zombie to the left of target go right
if (!facingRight) { //Check if needs flipping
flipZombie ();
}
zombieRB.velocity = new Vector2 (moveSpeed, zombieRB.velocity.y);
} else {
//zombie to the right of target go left
if (facingRight) { //Check if needs flipping
flipZombie ();
}
zombieRB.velocity = new Vector2 (moveSpeed * -1, zombieRB.velocity.y);
}
}
/// <summary>
/// Flips the zombie when moving
/// </summary>
void flipZombie() {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
And then I have a pulserZombie.cs but there is nothing in it but the inheritance:
public class pulserMovement : Zombie {
Here is my image of the prefab I have: