- Home /
Inheritance with enemies
Heyyo guys ! So i've created this code right here, and i wanted to do it so my enemies can inherit the movement and rotation code i've created. But, since im not good with inheritance and so forth it gives me all sorts of errors.
using UnityEngine; using System.Collections;
public class Enemies : MonoBehaviour {
Transform playerTransform;
Transform myTransform;
protected int Health;
protected int MoveSpeed;
protected int Attack;
protected int MovementSpeed;
protected int RotationSpeed;
protected float DetectionRange;
void Awake() {
myTransform = transform;
}
void Start () {
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update () {
if (Vector3.Distance(playerTransform.position, myTransform.position) < DetectionRange)
{
//Rotate to player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(playerTransform.position - myTransform.position), RotationSpeed * Time.deltaTime);
//Move to player
myTransform.position += myTransform.forward * MovementSpeed * Time.deltaTime;
}
}
}
Now these all works well if a put this script onto a gameobject, and give values to movementspeed, detection and rotation. But the moment i do this:
void Update () {
movement();
}
void movement () {
if (Vector3.Distance(playerTransform.position, myTransform.position) < DetectionRange)
{
//Rotate to player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(playerTransform.position - myTransform.position), RotationSpeed * Time.deltaTime);
//Move to player
myTransform.position += myTransform.forward * MovementSpeed * Time.deltaTime;
}
}
(I moved my movement and rotation code in a separate function). The reason for why im doing this is, that enemies should inherit these traits, and therefore making it easier for me to create enemies my adjusting the variables RotationSpeed, MovementSpeed and DetectionRange for each enemy that inherits this. In general i want this function problem solved, so i can begin to create script that inherit these traits properly, but i'm having a hard time figuring it out. Could you help me ?
Kind Regards, Lars.
Answer by PouletFrit · Jun 02, 2014 at 03:20 PM
If you want that each enemy have its own movement function you just have to declare a virtual method on the parent class.
protected virtual void movement() {
// Your basic rotation and movement code here
// This function will be used if you't dont override it in child classes
}
And then in your child class, just override this function
public class Slime : Enemies {
protected override void movement() {
// Movement code that is specific to this enemy type
}
}
Also, if you wish to call the function of the parent inside the child function just use:
base.movement();
Also, I have noticed that you didnt specify the access modifier (public, private, protected, etc..) for the variables playerTransform and myTransform, which mean that by default in c# they will be private and thus not accessible in your child classes. If you wish to use them in your child classes, switch them to protected.
Hey ! Thanks that worked a bunch ! I missed the whole private/public on my transform variables when trying to do it.
$$anonymous$$ind Regards, Lars.