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 jatmdm · Oct 30, 2013 at 12:59 AM · looptrigonometrydivision

Division in loop not working

I am trying to spawn 3 GameObjects around another GameObject with equal spacing between them. So far I have this:

 var i : float;
     for(i = 0; i < 3; i++){
         var rot : float;
         var spawnPos : Vector3;
         rot = ((i+1/3) * (Mathf.PI * 2));
         Debug.Log(i);
         Debug.Log(rot);
         
         spawnPos = new Vector3(5*Mathf.Cos(rot) + transform.position.x,
         5*Mathf.Sin(rot) + transform.position.y,
          transform.position.z);
          
          
         var g : GameObject = Instantiate(gunPreFab, spawnPos, Quaternion.identity);         
         guns.Add(g.gameObject);
         
         
         
     }

The variable "i" is being increased every loop cycle, however, "rot" never becomes anything other than zero. I am not sure at all why this is occurring.

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 rutter · Oct 30, 2013 at 01:13 AM 1
Share

If I run this:

 function Start() {
     var i : float;
     for (i=0; i<3; i++) {
         var rot : float;
         rot = ((i+1/3) * ($$anonymous$$athf.PI * 2));
         Debug.Log(rot);
     }
 }

I get 0, 6.3, 12.6.

avatar image robertbu · Oct 30, 2013 at 01:21 AM 0
Share

Good catch. The 1/3 will always evaluate to 0, but he's not getting 0 every time through the loop.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Oct 30, 2013 at 01:07 AM

Try this:

 rot = ((i+1.0/3.0) * (Mathf.PI * 2.0));

You were doing integer division.

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 jatmdm · Oct 30, 2013 at 01:31 AM 0
Share

This solves the problem of the rotation, but the spawning position always equals the same thing, no matter what "rot" is.

avatar image robertbu · Oct 30, 2013 at 01:40 AM 0
Share

You are multiplying by 2 * #PI, so the result will always be the same rotation. That is, 2.09, 8.378, and 14.66 all represent the same end angle.

avatar image jatmdm · Oct 30, 2013 at 01:50 AM 0
Share

Okay, so I'm doing my math wrong? What I'm trying to achieve is that the angle of rotation on them are 1/3 of 2(PI), 2/3 of 2(PI), and 2(PI), but apparently it's doing the first one fine and multiplying the rest by 2?

avatar image robertbu · Oct 30, 2013 at 01:56 AM 0
Share

You want to multiply:

 rot = ((i*1.0/3.0) * ($$anonymous$$athf.PI * 2.0));

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

16 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

Related Questions

Nested for-loop freezing Unity 1 Answer

Turning On & Off Renderers 1 Answer

Creating a looping spawn with an Array 0 Answers

Which Method is Optimal/Fastest - Raycast Vs Collider 3 Answers

How to obtain a gameObject from an List with the name? 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