Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Chris 31 · Mar 15, 2011 at 07:33 PM · collisioninstantiatetriggerlookatarrow

Making a turret fire another arrow if an enemy survives the hit

Hey, I made a script which is attached to a turret,

public class TurretRealScript : MonoBehaviour {

public GameObject arrowPrefab; public GameObject Target;

void Start () {

}

// Update is called once per frame void Update () {

} void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Enemy") { Target = other.gameObject; Instantiate(arrowPrefab, transform.position,transform.rotation); } }

}

and a script attached to the object which is to be fired at the enemy once the collision is triggered

public class ArrowProjectileScript2 : MonoBehaviour {

   public GameObject Target;
   private Vector3 direction = Vector3.forward;
   public float speed=30.0f;


// Use this for initialization void Start () {

}

// Update is called once per frame void Update () { Target = GameObject.FindWithTag("Enemy"); transform.Translate(direction*speed*Time.deltaTime); transform.LookAt(Target.transform); }

}

So far this works fine, when the turret sphere detects a collision the arrow is fired at the enemy and my script attached to the enemy destroys the arrow on collision but must require a further 2 hits before the enemy itself is destroyed,

public class Enemy1DestroyScript : MonoBehaviour {

public int count=0; // Use this for initialization void Start () {

}

// Update is called once per frame void Update () { if (count>2) { Destroy(gameObject); } } void OnCollisionEnter(Collision hit) { if(hit.gameObject.tag=="Arrow") { Destroy(hit.gameObject); count++; } }

}

What I would like to happen is after the first 2 secs of the arrow being fired, if there is an enemy still alive in the collison area then fire another arrow. currently an arrow is only fired when an a new enemy triggers the onTriggerEnter which is no use if some enemies require more than one arrow to be killed.

Thanks for your time!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cuddlybunny · Jun 01, 2012 at 02:34 PM

 var Target : GameObject;
 var bullet : GameObject;
 var fired = 0;
 
 function Start () { 
 }
 
 function Update () {
    if (Target){
       fireagain ();
    }
 }
 function OnTriggerEnter (other : Collider) {
         if (other.gameObject.tag == "enemy"){
               Target = other.gameObject;
               Instantiate(bullet,transform.position,transform.rotation);
               fired = 1;
         }
 }
 function fireagain (){
     if (fired == 0){
         fired = 1;
         Instantiate(bullet,transform.position,transform.rotation);
            yield WaitForSeconds (1);
         fired = 0;
     }
 }
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
avatar image
0

Answer by cuddlybunny · Jun 01, 2012 at 02:01 PM

Its Java but few changes should make it work.

 var Target : GameObject;
 var bullet : GameObject;
 var fired = 0;
 var firerate = 2;
 
 function Start () {
 }
 
 function Update () {
   if (Target)
   {
     fireagain ();
   } 
 }

 function OnTriggerEnter (other : Collider) {
         if (other.gameObject.tag == "enemy"){
                 Target = other.gameObject;
                 fireagain ();
         }
 
 }
 function fireagain (){
     if (fired == 0)
     {
         fired = 1;
         Instantiate(bullet,transform.position,transform.rotation);
         yield WaitForSeconds (firerate);
         fired = 0;
     }
 }
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
avatar image
0

Answer by fafase · Jun 01, 2012 at 12:28 PM

OnTriggerEnter only returns true on the frame when the enemy is entering the collider.

If you have new enemies instantiated at runtime, you can launch a function that would check for new enemies with FindGameObjectsWithTag, I would recommend to use every 1 or 2 sec as it is a very expensive function.

If they are all on the map at the beginning, just check for them in the Start function

Once you have those enemies in a list or built-in array of game object, you can check the distance with Vector3.Distance and check if the enemies are in a define distance and attack the closest one. Once the closest one is dead, the turret will check again for closest one and so on.

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

2 People are following this question.

avatar image avatar image

Related Questions

Affect only one clone from multiple clones? 1 Answer

Having a door open/disappear when collided with a specific particle... 1 Answer

Instantiated Prefab's script doesn't work 2 Answers

trigger replace prefab with another... 0 Answers

Collision between two instances of the same object 1 Answer


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