- Home /
enemy shooting bullet
Good morning. I want to make top down space game. I made a player and an enemy but the problem is that I can't make the bullets continue going forward, I only can make it stop at the point which the player was when it was summoned! need help please.
here is the bullet script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
[SerializeField] GameObject explosionPrefab;
[SerializeField] float speed;
float finalSpeed;
GameObject player;
Vector3 currentPlayerPos;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
if (player)
currentPlayerPos = player.transform.position;
finalSpeed = speed * Time.deltaTime;
}
void Update()
{
transform.LookAt(currentPlayerPos);
transform.position = Vector3.MoveTowards(transform.position, currentPlayerPos, finalSpeed);
if (transform.position == currentPlayerPos)
{
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
$$anonymous$$ove everything in your Start()
method after player = GameObject.FindGameObjectWithTag("Player");
into the Update()
You are setting values once on script start and they will change during gameplay
Answer by Ben_The_boxer · May 24, 2019 at 12:43 PM
Try mine if you like, its up and running though i am sure its far from perfect but it does have a timer to explode which you might like on your space shooter. I am also making one myself so feel free to ask me any other questions that might pop up
using UnityEngine;
using System.Collections;
public class Missile : MonoBehaviour
{
public Transform target;
public float speed = 70f;
public int damage = 50;
public float explosionRadius = 0f;
public GameObject exploEffect;
public float proximity;//<-----------I tried to implement this as a means of detonation without colliders, no luck yet
public float missileTime;
// Update is called once per frame
void Start()
{
//if (target == null))
target = GameObject.FindWithTag("Player").transform;
}
void Update()
{
if (target == null)
{
Destroy(gameObject);
return;
}
Vector3 dir = target.position - transform.position;
float distanceThisFrame = speed * Time.deltaTime;
if (dir.magnitude <= distanceThisFrame + proximity)
{
HitTarget();
return;
}
if (missileTime <= 0f)
{
HitTarget();
}
missileTime -= Time.deltaTime;
transform.Translate(dir.normalized * distanceThisFrame, Space.World);
transform.LookAt(target);
}
private void OnCollisionEnter(Collision collision)
{
HitTarget();
}
void HitTarget()
{
GameObject effectIns = (GameObject)Instantiate(exploEffect, transform.position, transform.rotation);
Destroy(effectIns, 5f);
if (explosionRadius > 0f)
{
Explode();
}
Destroy(gameObject);
}
void Explode()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
foreach (Collider collider in colliders)
{
if (collider.tag == "Player")
{
collider.gameObject.GetComponent<PlayerHealth>().DamagePlayer(damage);
//Damage(collider.transform);
}
Destroy(gameObject);
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, explosionRadius);
}
}
if you would prefer to stick with yours id recommend moving some bits from the "start" to "Update" and look into using "player.transform.position" as there is no need to manually extract the position when using that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612202020im_/https://answers.unity.com/themes/thub/images/avi.jpg)