Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by adapap · Mar 24, 2018 at 11:13 PM · optimizationgeometrysyntaxcubesradial

Techniques for performance and issue with polar calculations

I am trying to figure out the fundamentals of Unity by making what I thought was a simple project. The idea is that you have a square grid of cubes, and parts of the grid would burst up in sync with music to create an interesting audio visualizer. I am trying to get it working at random points and will do the music part later. Right now, it works perfectly on some settings, but when the circle "radius" gets too big, it doesn't appear to work correctly. The effect that i'm going for is similar to this video (but with a fixed focal point, it should not oscillate but rather go through one cycle): Example video

I also want to make sure since i'm learning Unity that I am being efficient in how I do things. Right now I have a rough idea of C# syntax, so for simplicity I iterate over two (2D?) arrays. There are probably better techniques to do what I am doing, so if you can provide optimizations for a beginner that would be very helpful. I am not sure why the radial ripple effect doesn't work on large values (try 15 gridsize, 3 cubesize, 1.5 growth interval, 25 growth size, and 5 shrink rate to see what I mean). Here is my entire code, which is attached to a standalone object:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CubeGenerator : MonoBehaviour {
 
     public GameObject whiteCube;
     public GameObject blackCube;
 
     public int gridSize;
     public int cubeSize;
 
     public float growthInterval;
     public float growthSize;
     public float shrinkRate;
 
     private GameObject[,] cubes;
     private bool[,] grownCubes;
     private int shrinkScale;
 
     void Start () {
 
         shrinkScale = 3;
 
         //Scale the cubes based on the grid and cube size
         whiteCube.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
         blackCube.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
 
         cubes = new GameObject[gridSize, gridSize];
         grownCubes = new bool[gridSize, gridSize];
 
         for (int x = 0; x < gridSize; x++) {
             for (int z = 0; z < gridSize; z++) {
                 if (x % 2 == z % 2) {
                     cubes[x, z] = (GameObject)Instantiate(whiteCube, new Vector3 (x * cubeSize, (float)cubeSize / 2, z * cubeSize), Quaternion.identity);
                 } else { 
                     cubes[x, z] = (GameObject)Instantiate(blackCube, new Vector3 (x * cubeSize, (float)cubeSize / 2, z * cubeSize), Quaternion.identity);
                 }
                 grownCubes[x, z] = false;
             }
         }
     }
     
     //Randomly grow a cube at this interval (seconds)
     float nextTime = 0;
     void Update () {
 
         if (Time.time >= nextTime)
         {
             int magnitude = (int)growthSize / cubeSize;
             Vector3 initialPosition = transform.position;
             Vector3 growth = new Vector3(0, growthSize, 0);
             int randomX = Random.Range(0, gridSize);
             int randomZ = Random.Range(0, gridSize);
 
             //ScaleObject(cubes[randomX, randomZ], growth, 1);
             for (int radius = 0; radius < magnitude; radius++)
             {
                 if (radius > 0)
                 {
                     int angleDelta = (int)(360 / (4 * radius));
                     for (int degrees = 0; degrees < 360; degrees += angleDelta)
                     {
                         int x = (int)(radius * Mathf.Cos(degToRad(degrees))) + randomX;
                         int z = (int)(radius * Mathf.Sin(degToRad(degrees))) + randomZ;
                         if (x >= 0 && x < gridSize && z >= 0 && z < gridSize && grownCubes[x, z] == false)
                         {
                             grownCubes[x, z] = true;
                             ScaleObject(cubes[x, z], growth * Mathf.Pow((float)2 / 3, (float)(radius)), 1);
                         }
                     }
                 }
                 else
                 {
                     ScaleObject(cubes[randomX, randomZ], growth, 1);
                 }
             }
 
             nextTime += growthInterval;
         }
 
         //Always shrink cubes if they are over the initial size
         foreach (GameObject cube in cubes)
         {
             if (cube.transform.localScale.y > cubeSize)
             {
                 Vector3 shrink = new Vector3(0, Mathf.Min(shrinkRate, cube.transform.localScale.y - cubeSize) * Time.deltaTime * shrinkScale, 0);
                 ScaleObject(cube, shrink, -1);
             }
         }
         for (int x = 0; x < gridSize; x++) {
             for (int z = 0; z < gridSize; z++) {
                 grownCubes[x, z] = false;
             }
         }
     }
 
     void ScaleObject (GameObject obj, Vector3 scalar, int direction)
     {
         obj.transform.localScale += direction * scalar;
         obj.transform.position += (direction * scalar) / 2.0f;
     }
 
     float degToRad (float degree)
     {
         return (float)degree * Mathf.PI / 180;
     }
 }
 

Thank you for bearing with me.

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

0 Replies

· Add your reply
  • Sort: 

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

133 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

Related Questions

One FBX with whole map from 3D soft or create map in unity ? 1 Answer

Unity 2D build for android, memory issue. Sprite import? 0 Answers

Is there a performance increase from using point textures instead of Bi/Trilinear textures? 0 Answers

WebGl RAM requirements 1 Answer

Lag Spike using Particle System Emit 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