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 cj31387 · Oct 10, 2012 at 10:54 PM · ailogic

Unique prefabs?

I have a script of formations that my enemy ships go to but they go to a prefab called "4 Slot Formation" I want to have like 10 of these on the map and my enemies go to the formations that are empty till all are full of their slots, I've got the logic on how they go to 1 of them, but I don't really know how to make them go to other ones because they are not unique, and or I don't know how to script them to all work together. Here is my script of how the fighters go to the "4 Slot Formation"

 using UnityEngine;
 using System.Collections;
 
 
 public class Fighter_AI : MonoBehaviour {
     
     
     
     // Access Formation AI
     public GameObject script;  // Access for getcomponent<Formation_AI>
 
     // Smooth Move to formation
     public float smoothTranslateSpeed = 0.3F;  // Speed for going from spawn to formation with lerp
 
     public Transform playerTarget;      //Target for the enemies to target the player and lookat
     public Transform playerDist;        // Going to use this for mutants they will freely come to the player but always stop at a certain distance, maybe it can be a random range too so it looks interesting
     public Transform fightFormTransform;   // Think this is for the formation transforms
 
     // Setup for the sockets
     public Transform FormTarg1;  // Formation socket1
     public Transform FormTarg2;  // Formation socket2
     public Transform FormTarg3;  // Formation socket3
     
 
     // Variables
     public bool emtyS1;
     public bool goToSlot01 = false;  // For the logic
     public bool goToSlot02 = false;  // For the logic
     public bool goToSlot03 = false;  // For the logic
 
     // Use this for initialization
     void Start () {
       playerTarget = Player_Controller.playerTransform;    // The player target declaration
       playerDist = Player_Controller.playerTransform;      // Player Distance declaration
       fightFormTransform = Formation_AI.FormTarget;        // formation target declaration
        
       // Targets for the Sockets  
       FormTarg1 = Formation_AI.Form_Slot1;  
       FormTarg2 = Formation_AI.Form_Slot2;
       FormTarg3 = Formation_AI.Form_Slot3;
      
  
 
     }
 
     void Awake()
     {
         script = GameObject.Find("4 Slot Formation");
         //Formation_AI script = GameObject.Find("4 Slot Formation").GetComponent<Formation_AI>();
 
         if (script.GetComponent<Formation_AI>().isEmptySlot1)
         {            
             script.GetComponent<Formation_AI>().isEmptySlot1 = false;
             
             goToSlot01 = true;
             goToSlot02 = false;
             goToSlot03 = false;
         }
 
         else if (script.GetComponent<Formation_AI>().isEmptySlot2)
         {
             
             script.GetComponent<Formation_AI>().isEmptySlot2 = false;
             
             goToSlot01 = false;
             goToSlot02 = true;
             goToSlot03 = false;
 
         }
         else if (script.GetComponent<Formation_AI>().isEmptySlot3)
         {
             
             script.GetComponent<Formation_AI>().isEmptySlot3 = false;
             
             goToSlot01 = false;
             goToSlot02 = false;
             goToSlot03 = true;
         } 
         
                 
     }
 
     
 
     // Update is called once per frame
     void Update () {
         transform.LookAt(playerTarget);
                        
         if (playerDist)
         {
             float dist = Vector3.Distance(playerDist.position, transform.position);
            //Debug.Log("Distance to other: " + dist);
         }
 
         if (goToSlot01)
         {
             GoToSlot01();
         }
         else if (goToSlot02)
         {
             GoToSlot02();
         }
         else if (goToSlot03)
         {
             GoToSlot03();
         }
 
     }
 
     // Moves the ships to the right spots. May need to replace the target with a variable that gets adjusted by how many formations are added
     void GoToSlot01()
     {
         transform.position = Vector3.Lerp(transform.position, FormTarg1.position, Time.deltaTime * smoothTranslateSpeed);
     }
 
     void GoToSlot02()
     {
         transform.position = Vector3.Lerp(transform.position, FormTarg2.position, Time.deltaTime * smoothTranslateSpeed);
     }
 
     void GoToSlot03()
     {
         transform.position = Vector3.Lerp(transform.position, FormTarg3.position, Time.deltaTime * smoothTranslateSpeed);
     }
 }
 
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 Michael Covert · Oct 11, 2012 at 12:27 AM

I'd use GameObject.FindGameObjectsWithTag() to find all of your formations (of course, you'd have to tag your formations). This returns an array of all your formation variables; you can then proceed to cycle through your array until you find an empty spot and then assign your fighter to that slot.

Something like:

 GameObject[] allSpots = GameObject.FindGameObjectsWithTag("Formations");
 for(int i = 0; i < allSpots.Length; ++i)
 {
      if( /* your logic to determine if a spot is free */ )
      {
           //assign that spot
           break;
      }
 }

Does that make sense?

Also, I'd avoid calling GameObject.FindGameObjectsWithTag() more often than you need to; maybe store this in a variable that only gets updated when new formations are created - but that's something you can worry about more once you have your base functionality working.

Comment
Add comment · Show 4 · 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 cj31387 · Oct 11, 2012 at 01:08 AM 0
Share

Yeah that somewhat makes sense, but How do i access duplicate variables from duplicate prefabs that way? script.GetComponent().isEmptySlot1 seems to be all the formations ins$$anonymous$$d of specific ones.

avatar image Michael Covert · Oct 11, 2012 at 01:13 AM 0
Share

you'd do something like allSpots[i].GetComponent().isEmptySlot1

avatar image cj31387 · Oct 11, 2012 at 01:25 AM 0
Share

Hmm thanks, that almost worked, now I just need to figure out changing to the other transform targets of the next formation, I would guess the same thing except with transforms of the spawn sockets I want?

avatar image Michael Covert · Oct 11, 2012 at 01:59 AM 0
Share

That should work, yeah - good luck!

avatar image
1

Answer by clakes707 · Oct 10, 2012 at 10:58 PM

I think what you would want to do is set up a system of checks with variables. When a ship is added to the first prefab, set it counter variable to + 1. When it reaches its max, make the ships go to the next prefab, and so on.

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 cj31387 · Oct 10, 2012 at 11:23 PM 0
Share

I don't even know how to find the next prefab as unique though? Because they have the same exact name. It needs to be dynamic too because I'm going to randomly spawn these formations on pre placed sockets all around the map at certain times.

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

11 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

Related Questions

ai logic in javascript 1 Answer

[Unity Multiplayer Networking] About NPCs/AIs logic (movement, interaction,..) in multiplayer networking 0 Answers

Generate tiles along a path a certain distance before reaching end position 1 Answer

Basic AI finite state machine: where to put decision logic? 2 Answers

Good refernce material for logic behind ai 0 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