- Home /
Question by
Blogan · Nov 25, 2016 at 07:56 AM ·
scripting problem
Enemies not dying and bullet not exploding and particle system not being made.
I am making a tower defense game and everything works fine until the weapon shoots at the enemy. When the gun shoots, the enemy(gameObjcet) is supposed to be destroyed. The bullet is also supposed to explode into a particle system that I made. I am following a youtube tutorial and i have check my code several times. Here is the bullet code: using UnityEngine;
public class Bullet : MonoBehaviour {
private Transform target;
public float speed = 70f;
public GameObject ImpactEffect;
public void Seek (Transform _target)
{
target = _target;
}
// update is called once per frame
void Update () {
if (target == null)
{
Destroy (gameObject);
return;
}
Vector3 dir = target.position - transform.position;
float distanceThisFrame = speed * Time.deltaTime;
if (dir.magnitude <= distanceThisFrame)
{
HitTarget ();
return;
}
transform.Translate (dir.normalized * distanceThisFrame, Space.World);
}
void HitTarget ()
{
GameObject effectIns = (GameObject) Instantiate (ImpactEffect, transform.position, transform.rotation);
Destroy (effectIns, 2f);
Destroy (target.gameObject);
Destroy (gameObject);
}
}
Comment
Your answer
