Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DankGameGenerator · Oct 04, 2017 at 06:26 AM · bulletturretfly

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tu-Le · Oct 05, 2017 at 04:22 AM 0
Share

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.

avatar image DankGameGenerator Tu-Le · Oct 05, 2017 at 07:15 AM 0
Share

Thanks man! It seems to be working now!! :D Cheers! ( i gave you 3+ Rep points, you earned 'em ;D )

avatar image DankGameGenerator DankGameGenerator · Oct 05, 2017 at 07:23 AM 0
Share

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?

Show more comments
avatar image Tu-Le · Oct 05, 2017 at 04:34 AM 0
Share
 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);
     }
 }
 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges