• 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 haimmoshe · Sep 06, 2017 at 03:40 AM · c#scripting problemscript.

How can i use a button key click to switch/toggle between enum options ?

I want that once i click on F it will call Formation Square and another click on F will call the Formation Circle then another click on F Square,Circle....

And when it's calling/using Square to call/use the method FormationSquare and when Circle then calling/using RandomCircle.

Instead the switch/case to use If/Else the enum and the key code F. Or maybe using switch/case is better but the idea is to use the key F for toggling between FormationSquare and RandomCircle.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SquadFormation : MonoBehaviour
 {
     enum Formation
     {
         Square, Circle
     }
 
     public Transform squadMemeber;
     public int columns = 4;
     public int space = 10;
     public int numObjects = 20;
     public float yOffset = 1;
 
     // Use this for initialization
     void Start()
     {
         ChangeFormation();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.F))
         {
 
         }
     }
 
     private void ChangeFormation()
     {
         Formation formation = Formation.Circle;
 
         switch (formation)
         {
             case Formation.Square:
 
                 for (int i = 0; i < 23; i++)
                 {
                     Transform go = Instantiate(squadMemeber);
                     Vector3 pos = FormationSquare(i);
                     go.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
                     go.Rotate(new Vector3(0, -90, 0));
                 }
                 break;
 
             case Formation.Circle:
 
                 Vector3 center = transform.position;
                 for (int i = 0; i < numObjects; i++)
                 {
                     Vector3 pos = RandomCircle(center, 5.0f);
                     var rot = Quaternion.LookRotation(pos - center);
                     pos.y = Terrain.activeTerrain.SampleHeight(pos);
                     pos.y = pos.y + yOffset;
                     Instantiate(squadMemeber, pos, rot);
                 }
                 break;
         }
     }
 
     Vector2 FormationSquare(int index) // call this func for all your objects
     {
         float posX = (index % columns) * space;
         float posY = (index / columns) * space;
         return new Vector2(posX, posY);
     }
 
     Vector3 RandomCircle(Vector3 center, float radius)
     {
         float ang = Random.value * 360;
         Vector3 pos;
         pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
         pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
         pos.y = center.y;
         return pos;
     }
 }
 
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
1
Best Answer

Answer by unit_nick · Sep 06, 2017 at 05:48 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SquadFormation : MonoBehaviour
 {
     enum Formation
     {
         Square, Circle
     }
 
     public Transform squadMemeber;
     public int columns = 4;
     public int space = 10;
     public int numObjects = 20;
     public float yOffset = 1;
 
     // *NEW* create global variable
     private Formation formation;
 
     // Use this for initialization
     void Start()
     {
         // *NEW* initialize formation
         formation = Formation.Square;
         ChangeFormation();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.F))
         {
             // *NEW* 
             ChangeFormation();
         }
     }
 
     private void ChangeFormation()
     {
         // *NEW* delete the local variable declaration
         // Formation formation = Formation.Circle;
 
         switch (formation)
         {
             case Formation.Square:
 
                 for (int i = 0; i < 23; i++)
                 {
                     Transform go = Instantiate(squadMemeber);
                     Vector3 pos = FormationSquare(i);
                     go.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
                     go.Rotate(new Vector3(0, -90, 0));
                 }
                 // *NEW* toggle formation
                 formation = Formation.Circle;
                 break;
 
             case Formation.Circle:
 
                 Vector3 center = transform.position;
                 for (int i = 0; i < numObjects; i++)
                 {
                     Vector3 pos = RandomCircle(center, 5.0f);
                     var rot = Quaternion.LookRotation(pos - center);
                     pos.y = Terrain.activeTerrain.SampleHeight(pos);
                     pos.y = pos.y + yOffset;
                     Instantiate(squadMemeber, pos, rot);
                 }
                 // *NEW* toggle formation
                 formation = Formation.Square;
                 break;            
         }
     }
 
     Vector2 FormationSquare(int index) // call this func for all your objects
     {
         float posX = (index % columns) * space;
         float posY = (index / columns) * space;
         return new Vector2(posX, posY);
     }
 
     Vector3 RandomCircle(Vector3 center, float radius)
     {
         float ang = Random.value * 360;
         Vector3 pos;
         pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
         pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
         pos.y = center.y;
         return pos;
     }
 }



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 unit_nick · Sep 06, 2017 at 05:51 AM 0
Share

This might cycle through the formations too quickly. You may need to check that a certain amount of time has passed before changing the formation again.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

397 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 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 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 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 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 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

How can i Instantiate on the terrain from left to right ? 0 Answers

Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer

How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers

How can i spawn new gameobjects to be inside the terrain area ? 2 Answers

How can i change vertex posiiton to push it back -1 on z ? 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