Doing a certificate course in Game Development and the code that we’ve been given hasn’t been working properly for me, mainly the projectile firing script for the main player and the enemies, this here is for the enemies yet, the main player’s script for firing is relatively the same,
The main problem I’m have here is that the enemy doesn’t actually fire until it’s in contact with me and when it is it spawns continous copies of projectile that are invisible yet in the hierachy.
The main player I can’t even get the projectile to appear in the hierachy at all
Could I get some help please guys :D!
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public GameObject ExplosionType;
public float RespawnTimer = 10.0f;
public int Health = 100;
public float StopDistance = 5.0f;
public float MovementSpeed = 5.0f;
public float ProjectileForwardForce = 10;
public float ProjectileUpForce = 10;
public GameObject objectToSpawn;
float CurrentTimer;
GameObject ParticleExplosion;
bool isAlive = true;
// Use this for initialization
void Start () {
CurrentTimer = RespawnTimer;
}
// Update is called once per frame
void Update () {
if ( isAlive == false)
{
CurrentTimer -= Time.deltaTime;
if (CurrentTimer < 0)
{
Destroy( ParticleExplosion );
Destroy ( gameObject );
}
}
else
{
if(Health <= 0)
{
Explode();
}
GameObject tank = GameObject.FindGameObjectWithTag("Player");
if (tank != null)
{
// work out new facing direction for tank
Quaternion rotation = Quaternion.LookRotation(tank.transform.position - transform.position);
// slowly rotate to new facing
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
//get distance between enemy and player for when to stop
float distance = Vector3.Distance(tank.transform.position, transform.position);
if (distance > StopDistance)
{
transform.Translate (transform.forward * MovementSpeed * Time.deltaTime);
}
else
{
Vector3 offsetForward = -transform.up * 1;
Vector3 offsetUp = transform.forward * 0.5f;
Vector3 finalPosition = transform.position + offsetForward + offsetUp;
GameObject projectile = Instantiate(objectToSpawn, finalPosition, transform.rotation) as GameObject;
projectile.rigidbody.AddRelativeForce(projectile.transform.forward * -ProjectileForwardForce, ForceMode.Impulse);
projectile.rigidbody.AddRelativeForce(projectile.transform.up * -ProjectileUpForce, ForceMode.Impulse);
}
}
}
}
public void Hit(int Damage)
{
Health -= Damage;
}
/// Explode this instance.
public void Explode ()
{
ParticleExplosion = Instantiate(ExplosionType, transform.position, transform.rotation) as GameObject;
isAlive = false;
}
}