Hello, I am very new to Unity and I'm working on a project that is requiring a polymorphic collection of enemies (3) and weapons(3). I must have an enemy class with three subclasses which have to implement a move and an attack method specific to its' behavior. So far I have my three enemies and one is weakest, medium, and strong with the weaker ones moving slower and have less of a look and agro range than the higher enemy. The variables specific to this are agro range, look range and speed. I am not sure my implementation with the virtual methods and polymorphism is correct. They all inherit the variables and I am able to change them under their individual enemy spots in the unity transform but I feel like I did this incorrectly and I am looking for any help, thank you.
**The strange thing is everything compiles, and when I play my game, the enemies actually move AWAY from the player. That's the part I'm struggling to understand most .**
Another requirement is that **all hard coded enemies and weapons must be removed.** Will using polymorphism remove that?
**Also, the public float distance from_player is not supposed to be public, but in order for each of my subclasses to inherit that variable it has to be. How can I change that?**
EnemyBase Class
using UnityEngine;
using System.Collections;
using System;
public class EnemyBase : MonoBehaviour
{
public Transform Player;
public float move_speed;
public float look_range;
public float distancefrom_player;
public float agro_range;
protected Vector3 vel;
public virtual void MoveTo ()
{
if(transform==null)
return;
transform.position += vel;
}
void Start ()
{
}
public virtual void Update ()
{
distancefrom_player = Vector3.Distance (Player.position, transform.position);
if (distancefrom_player > look_range )
{
transform.LookAt(Player);
}
if (distancefrom_player > agro_range)
{
attack();
}
}
Enemy1 Class
using UnityEngine;
using System.Collections;
using System;
public class EnemySub1 : EnemyBase {
void Start () {
}
public override void MoveTo ()
{
if(transform==null)
return;
transform.position += vel;
}
public override void attack()
{
transform.Translate (Vector3.back * move_speed * Time.deltaTime);
}
public override void lookAt()
{
Quaternion rotation = Quaternion.LookRotation (Player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
}
public override void Update ()
{
distancefrom_player = Vector3.Distance (Player.position, transform.position);
if (distancefrom_player < look_range )
{
transform.LookAt(Player);
}
if (distancefrom_player < agro_range)
{
attack();
}
}
}
Enemy2 Class
using UnityEngine;
using System.Collections;
using System;
public class EnemySub2 : EnemyBase {
void Start () {
}
public override void MoveTo ()
{
if(transform==null)
return;
transform.position += vel;
}
public override void attack()
{
transform.Translate (Vector3.back * move_speed * Time.deltaTime);
}
public override void lookAt()
{
Quaternion rotation = Quaternion.LookRotation (Player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
}
public override void Update ()
{
distancefrom_player = Vector3.Distance (Player.position, transform.position);
if (distancefrom_player < look_range )
{
transform.LookAt(Player);
}
if (distancefrom_player < agro_range)
{
attack();
}
}
}
Enemy3 Class
using UnityEngine;
using System.Collections;
using System;
public class EnemySub3 : EnemyBase {
void Start () {
}
public override void MoveTo ()
{
if(transform==null)
return;
transform.position += vel;
}
public override void attack()
{
transform.Translate (Vector3.back * move_speed * Time.deltaTime);
}
public override void lookAt()
{
Quaternion rotation = Quaternion.LookRotation (Player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
}
public override void Update ()
{
distancefrom_player = Vector3.Distance (Player.position, transform.position);
if (distancefrom_player < look_range )
{
transform.LookAt(Player);
}
if (distancefrom_player < agro_range)
{
attack();
}
}
}
↧