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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ash-Bash32 · May 31, 2013 at 01:35 PM · collisioncolliderontriggerenterenemies

Problems When Shooting Multiple Enemies round about 4 times

When I have Multiple Enemies they have a float number for health round about 250.0f so when i shoot at one of them that number should decrease by 50 each time i shoot at the enemy but heres my problem the first enemy dies round about 4/5 shots to it but when i start to shoot at the other (after the first enemy) It takes 1 shot and there dead so heres the Bullet script for the players Bullet called Bullet.cs:

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour {
     
     public GameObject BulletObject;
     public GameObject GlobelObjectExplosion;
     public GameObject GlobelBulletExplosion;
     public float life = 3.0f;
     private EnemyAI enemy;
 
     
     private Transform bulletTransform;
     // Use this for initialization
     void Start () {
     
     }
     
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "Enemy")
         {
             
             EnemyAI.currEnemyLife -= 50;
             Player.score += 50;
             
             //Creates an explosion on collision
             GameObject objBullet = Instantiate(GlobelBulletExplosion, transform.position, Quaternion.identity) as GameObject;
             Destroy(objBullet, 2.0f);
             
             Debug.Log("ColliderWorking");
             
             if(EnemyAI.currEnemyLife <= 0.0f)
             {
                 Destroy(other.gameObject);
                 
                 //creates an explosion when enemy is destoryed
                 GameObject obj = Instantiate(GlobelObjectExplosion, other.gameObject.transform.position, Quaternion.identity) as GameObject;
                 
                 Destroy(obj, 2.0f);
                 Player.score += 250;
             }
             Destroy(gameObject);
         }
     }
     
     // Update is called once per frame
     void Update () {
 
         life -= Time.deltaTime;
         
         transform.Translate(0, 0, Player.bulletSpeed * Time.deltaTime);
         
         if(life <= 0.0)
         {
             
             Destroy(gameObject);
             
         }
     
     }
 }

And heres the EnemyAI Script called EnemyAI.cs:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI: MonoBehaviour {
     
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     public GameObject explosion;
     
     public static float maxEnemyLife = 250.0f;
     public static float currEnemyLife;
     public static float maxEnemyBullets = 60.0f;
     public static float currEnemyBullets;
     public static float maxEnemyFuel = 1260.0f;
     public static float currEnemyFuel;
     
     private Transform myTranform;
     public static Transform LocalTransform;
     
     void Awake(){
     
         myTranform = transform;
         LocalTransform = myTranform;
         
     }
 
     // Use this for initialization
     void Start () {
         if(GameObject.FindGameObjectWithTag("Player") != null){
             GameObject go = GameObject.FindGameObjectWithTag("Player");
             
             currEnemyFuel = maxEnemyFuel;
             currEnemyLife = maxEnemyLife;
             currEnemyBullets = maxEnemyBullets;
     
             target = go.transform;
         }
         
         
     
     }
     
     // Update is called once per frame
     void Update () {
         if (currEnemyFuel > maxEnemyFuel)
         {
             currEnemyFuel = maxEnemyFuel;    
         }
     
             //Debug.DrawLine(target.position, myTranform.position, Color.cyan);
         if(currEnemyFuel <= 0.0)
         {
                 currEnemyFuel = 0.0f;
                 //myTranform.rotation = Vector3.zero;
                 //myTranform.position = Vector3.zero;
             moveSpeed = 0;
         }
         else if (currEnemyFuel > 0.0)
         {
             //Look at target
             if(GameObject.FindGameObjectWithTag("Player") != null){
                 myTranform.rotation = Quaternion.Slerp(myTranform.rotation, Quaternion.LookRotation(target.position - myTranform.position), rotationSpeed * Time.deltaTime);
             
                 //Move towards Target
                 myTranform.position += myTranform.forward * moveSpeed * Time.deltaTime;
                 currEnemyFuel -= 0.2f;
             }
             
         }
         
     
     }
 }


Ive tried to find if someone had the some problem or if theres a workaround but i can find a solution on here or google. So can someone tell me if im doing something wrong or is there a tutorial or something

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dorpeleg · May 31, 2013 at 03:38 PM

In your void OnTriggerEnter(Collider other) In the Bullet script, do the following changes:

After

 if(other.gameObject.tag == "Enemy"){

Add

 enemy = other.gameObject.GetComponent<EnemyAI>();

Then, every time you have EnemyAI.something, change it to enemy.something.

I think your problem is incorrect reference.

Comment
Add comment · Show 1 · 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 Ash-Bash32 · May 31, 2013 at 03:40 PM 0
Share

thanks ill try that

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

14 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

Related Questions

Cubes wont Collide 1 Answer

Collision Detection without a RigidBody 2 Answers

Collision not detected 3 Answers

Collision problem in C# 4 Answers

OnTrigger event when colliders are already touching eachother 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