- Home /
Is there any way to make bullets fly past targets if they miss?
I am at Episode 8 of Brackeys' Tower Defence Tutorial, and my perfectionism can't just let it slip by, that every bullet that is mid-travel, just disappears if the target dies from another bullet xD..
Anyone got some mad skills and want to help me?
The general idea i had was to make some sort of "if" statement, to then allow Bullets to fly past IF the target dies mid flight. Then, after 2 or seconds, they would just die outside of the map?
Answer by DankGameGenerator · Oct 04, 2017 at 09:18 PM
public class Bullet : MonoBehaviour
{
public float t = 3f;
private Transform target;
public float speed = 70f;
public GameObject impactEffect;
public void Seek(Transform _target)
{
target = _target;
}
// So Far, i have made the bullet fly to the Target, in a "homing" style way. If the target dies before the bullet can reach it, atm: it just stops for 3 seconds, then disappears
void Update()
{
if (target == null)
{
Destroy(gameObject, t);
return;
}
Vector3 dir = target.position - transform.position;
float distanceThisFrame = speed * Time.deltaTime;
if (dir.magnitude <= distanceThisFrame)
{
HitTarget();
return;
}
// Here we make sure that the speed of the bullets is stable.
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);
}
}
sorry, here's one thats not fucked
your code is almost complete, in this case you should backup your Dir vector3. and If Target die when the bullet flying, the bullet still move follow Dir vector3.
Thanks man! It seems to be working now!! :D Cheers! ( i gave you 3+ Rep points, you earned 'em ;D )
Just one more thing:
As it is now, with the new Vector3, the bullets do NOT have the "ho$$anonymous$$g" effect, when they have a target. What i was going for, was for the bullets to hit the target no matter what ( At first )
and then: if the target was destroyed, the bullets should continue flying in whatever direction they were heading to?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : $$anonymous$$onoBehaviour
{
public float t = 3f;
private Transform target;
Vector3 Dir;
public float speed = 70f;
public GameObject impactEffect;
public void Seek(Transform _target)
{
target = _target;
//We need a direction backup in case Target null
Dir = target.position - transform.position;
}
private void Awake()
{
Dir = Vector3.zero;
}
// So Far, i have made the bullet fly to the Target, in a "ho$$anonymous$$g" style way. If the target dies before the bullet can reach it, atm: it just stops for 3 seconds, then disappears
void Update()
{
float distanceThisFrame = speed * Time.deltaTime;
if (target == null)
{
// if there are no target from awake, the Dir will be set to zero(0,0,0), end if target disappear when bullet flying we have Dir with valued, just move follow Dir after destroy
if (Dir != Vector3.zero)
transform.Translate(Dir.normalized * distanceThisFrame, Space.World);
Destroy(gameObject, t);
return;
}
Vector3 dir = target.position - transform.position;
if (dir.magnitude <= distanceThisFrame)
{
HitTarget();
return;
}
// Here we make sure that the speed of the bullets is stable.
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);
}
}
Answer by Tu-Le · Oct 04, 2017 at 07:16 AM
it's depend on how you create your bullet. If you create a real bullet that fly to target, when target die and disappears the bullet's target will be set to null. and you can set what it will do when target is null
Your answer
![](https://koobas.hobune.stream/wayback/20220612140231im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Turret bullet rotation problem 1 Answer
NullReferenceException with a turret shooting a bullet 2 Answers
Set bullet direction according to turret direction 0 Answers
How to make Cannon ball fly in arc 2 Answers
Turret Firing problem [C#] 2 Answers