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 Aladine · Aug 14, 2013 at 06:26 PM · collisionprefabsarraylist

Problem with multi prefabs collision

Edit : more informations

Hello everyone,

am still trying to make an improved version of one of my old flash prototype and right now am in the part where the enemies should avoid each other, if you visited the link above, then you will notice that the enemies collide but they don't remain in the same position together, and this what i don't know how to achieve with unity, i have an enemy prefabs that have this script attached to it

 using UnityEngine;
 
 using System.Collections;
 
  
 
 public class StandEnemy : MonoBehaviour
 
 {
 
     
 
     
 
     private Camera      cam;                    //game camera 
 
     private Transform   myTransform;            //transform cache   
 
     
 
     public float speed ;
 
     private float currentSpeed;
 
     private Vector3 targetPosition;
 
  
 
     
 
     // Use this for initialization
 
     void Start ()
 
     {
 
     
 
         myTransform = transform;
 
         speed = 5;
 
         currentSpeed = speed;
 
         
 
         //initiating settings
 
         if (cam == null) {
 
             cam = Camera.main;
 
         
 
         }
 
     }
 
     
 
     // Update is called once per frame
 
     void Update ()
 
     {
 
         //follow the player
 
         float distance = myTransform.position.z - cam.transform.position.z; 
 
         targetPosition = new Vector3 (ShipControl.myPosition.x, ShipControl.myPosition.y, 0);
 
         myTransform.position = Vector3.MoveTowards (myTransform.position, targetPosition, currentSpeed * Time.deltaTime);
 
         
 
         
 
     }
 
     
 
     void OnTriggerEnter (Collider collider)
 
     {
 
         if (collider.gameObject.CompareTag ("stand_enemy")) {
 
             print ("you're touching your bro");
 
         }
 
     }
 
     
 
     void OnTriggerExit (Collider collider)
 
     {
 
         
 
         if (collider.gameObject.CompareTag ("stand_enemy")) {
 
             print ("bro is gone");
 
         }
 
     }
 
 }

and an empty game object that work as an enemies generator that have this code attached to it :

 using UnityEngine;
 
 using System.Collections;
 
  
 
 public class Enemies : MonoBehaviour
 
 {
 
  
 
     // Use this for initialization
 
     
 
     public GameObject   StandEnemyFab;              // bullet prefab
 
     public Vector3 randomPos;
 
     public float luck;
 
  
 
     void Start ()
 
     {
 
         
 
         for (int i=0; i<10; i++) {
 
             
 
             randomPos.x = Random.Range (-12, 12);
 
             luck = Random.Range (0, 6);
 
             if (luck < 3) {
 
                 randomPos.y = Random.Range (6, 8);
 
             } else {
 
                 randomPos.y = Random.Range (-8, -6);
 
             }
 
             randomPos.z = transform.position.z;
 
             Instantiate (StandEnemyFab, randomPos, transform.rotation);
 
         
 
         }
 
         
 
     }
 
     
 
     // Update is called once per frame
 
     void Update ()
 
     {
 
         
 
         
 
     
 
     
 
     }
 
 }

in the flash version the enemies were stored inside an arrayList and when a collision happens, enemiesList[i] collide with enemiesList[i+1], then only enemiesList[i] speed up or slow down, the other just keep his normal speed (yeah there is always an extra check) but i don't know how to do that with c#, and also am saying that maybe there is a better way to do it, so can you guys help please ?

thank you

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 IgorAherne · Aug 14, 2013 at 07:37 PM

you could actually add a second collider to the enemy, change it to trigger and work in OnTriggerEnter function in each enemies' script.

But if you still need to store each enemy in array (if in java script) and GenericList if in (C#), Generics are better, then do this in the EnemyGenerator's script:

JS:

   //EnemyGenerator
    #pragma strict
  
 public var enemiesArray : Array = newArray()
 private var newEnemy : GameObject;
 
 newEnemy = GameObject.Instantiate(blablabla);
 enemiesArray.Add(newEnemy);


later, when you need to refer from another script, you could refer to the needed cell in enemiesArray; for example, in c#

 //first establish the shortcut to script, somewhere in Start
   EnemyGenerator _EnemyGenerator = GameObject.Find("yourGeneratorGameObject").GetComponent<EnemyGenerator>()

 //then, use it anywhere, including the update
  _EnemyGenerator.enemiesArray[0] would mean you want to access the first enemy. 

newEnemy is a temporaral variable, that is used and re-written, but you still store the enemies in array as the GameObjects, each one sits on it's own cell.

for c#:

 using System.Collections;
 using UnityEngine;
 using System.Collections.Generic;
 
 public class EnemyGenerator{
 
  public List<GameObject> enemiesList = new List<GameObject>();
  private GameObject newEnemy;
 
  newEnemy = GameObject.Instantiate(blablabla);
  enemiesArray.Add(newEnemy);
 
 } 


Please check this as well :) http://forum.unity3d.com/threads/191923-Full-corse-how-to-make-a-horror-game-AVAILABLE

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 Aladine · Aug 14, 2013 at 11:02 PM 1
Share

thank you for replying,

you could actually add a second collider to the enemy, change it to trigger and work in OnTriggerEnter function in each enemies' script.

yes i tried that but the thing that i want to do exactly is, when 2 enemies collide to each other online one of them slow down so the other can pass or i will end up with all the enemies regrouped in the same place,

i will try your script when i get home, and by the way thank you for the link, i think it's gonna be very useful (i hope the game is in c# ) i will definitely check it when i finish my first project (simple 2d shooter) EDIT : oh you mentioned that the game is gonna be in C#, AWESO$$anonymous$$E ^_^ !! am subscribing to your channel thank you

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

17 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

Related Questions

PreFabs not colliding with other GameObjecs, but collide with themselves. 0 Answers

instantiate prefabs/gameObjects 1 Answer

Weird collision/physics problem. HELP,Weird physics/collision problem 1 Answer

How would I go about storing spawned prefabs to acces them ? 3 Answers

How to count collisions of the same tagged objects? 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