Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 question was closed Mar 04 at 06:48 AM by indricutandrei for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by indricutandrei · Mar 03 at 12:41 PM · mathinstantiate prefabcircular

I want to generate 2 stacks of 3 objects around a circle but I get 1 stack of 6

I have 2 functions which generate instances of objects around the attached object's transform:

 public void GenerateChairs()
     {
         Vector3 position;
         Vector3 orientation;
 
         float angle = 0.0F;
 
         for (int i = 0; i < Game.players.Count; ++i)
         {
             angle = i * Mathf.PI * 2.0F / Game.players.Count;
             position = new Vector3(Mathf.Cos(angle) * 0.01F, transform.position.y, Mathf.Sin(angle) * 0.01F);
             orientation = transform.position - position;
             GameObject instanceOfChair = Instantiate(chair, position, Quaternion.LookRotation(orientation));
         }
     }
 
     public void GeneratePlayers()
     {
         GameObject characterType;
         Vector3 position;
         Vector3 orientation;
 
         int numberOfGeneratedCharacters = 0;
         float angle = 0.0F;
 
         while (numberOfGeneratedCharacters < Game.players.Count - 1)
         {
             characterType = Game.humanColor == 0 ? characterSlime : characterTurtle;
             angle = - numberOfGeneratedCharacters * Mathf.PI * 2.0F / Game.players.Count;
             position = new Vector3(Mathf.Cos(angle) * 3.0F, 2.5F, Mathf.Sin(angle) * 3.0F);
             orientation = transform.position - position;
             GameObject computerCharacter = Instantiate(characterType, position, Quaternion.LookRotation(orientation));
             computerCharacter.name = "Player " + (numberOfGeneratedCharacters + 2).ToString();
             Game.characters.Add(computerCharacter);
             computerCharacter.transform.Rotate(-60.0F, 0, 0, Space.Self);
             numberOfGeneratedCharacters++;
         }
 
         angle = - numberOfGeneratedCharacters * Mathf.PI * 2.0F / Game.players.Count;
         characterType = Game.humanColor == 0 ? characterTurtle : characterSlime;
         position = new Vector3(Mathf.Cos(angle) * 3.0F, 2.5F, Mathf.Sin(angle) * 3.0F);
         orientation = transform.position - position;
         GameObject humanCharacter = Instantiate(characterType, position, Quaternion.LookRotation(orientation));
         humanCharacter.name = "Player 1";
         Game.characters.Insert(0, humanCharacter);
         humanCharacter.transform.Rotate(-60.0F, 0, 0, Space.Self);
     }

Basically I want Game.players.Count characters rendered 2.5F above Game.player.Count chairs, but in the end I get a sequence of PLAYER - CHAIR - PLAYER - CHAIR in a hexagon around the table. What did I do wrong with the math?

Comment
Add comment · Show 2
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 Bonfire-Boy · Mar 03 at 04:59 PM 2
Share

Not sure how to fix that particular code (because of strange differences between the 2 calculations, like the negative angle used in the second function) but it looks to me like you're creating an potential for error as well as a maintenance burden by unnecessarily duplicating stuff.


So I have a couple of alternative approaches to suggest...


First - if you want them in the same positions then write a function that'll give you those positions, rather than trying to duplicate the code that generates them. eg List<Vector3> GetCharacterPositionsForNPlayers(int numPlayers) (or it could return a List<Pose> if you want to include rotations). Then use the output of that function to get the positions in each of your functions (modifying the y-coordinates as required).


Or, and this is probably what I'd do... create one of the sets first and then use their positions to set the positions of the other set.


avatar image elenzil Bonfire-Boy · Mar 03 at 07:01 PM 0
Share

this is good advice.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

145 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

Related Questions

How to get 0-360 degree from two points 2 Answers

Calorie tracker 1 Answer

Check my math...? 1 Answer

Vertical Angle Between two game objects 2 Answers

Math Genius NEEDED! Algorithm problem with parabola 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