radial Instantiate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class radialInstantiate : MonoBehaviour { public GameObject obj; public int number; public float radius; public float Rspeed; private void Awake() { for(int i=0; i<number; i++) { float angle = i * Mathf.PI * 2/number; GameObject child = Instantiate(obj, new Vector3(transform.position.x + Mathf.Cos(angle) * radius, transform.position.y + Mathf.Sin(angle) * radius, transform.position.z), Quaternion.Euler(0,0,(360/number) * i - 90)); child.transform.SetParent(this.transform); } } private void Update() { //transform.rotation = Quaternion.Euler(0,0,Time.deltaTime * 100); transform.Rotate(0, 0, Time.deltaTime * Rspeed); } } | cs |
using System.Collections;using System.Collections.Generic;using UnityEngine;public class radialGenerator : MonoBehaviour{public GameObject prefab;public int prefabNum;public float radius;public float animSpeed;public bool animation = true;private void Awake(){for (int i = 0; i < prefabNum; i++){float angle = i * Mathf.PI * 2 / prefabNum;//원형으로 prefab생성GameObject child =Instantiate(prefab, new Vector3(transform.position.x + Mathf.Cos(angle) * radius,transform.position.y ,transform.position.z + Mathf.Sin(angle) * radius),Quaternion.Euler(0, -90 -((360/prefabNum)*i), 0));child.transform.SetParent(this.transform);}}private void Update(){//회전 animationif(animation){transform.Rotate(0, 0, Time.deltaTime * animSpeed);}}}
random Instantiate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class randomInstantiate : MonoBehaviour { public GameObject obj; public int number; private float time = 0; public float speed; private List<Transform> objs = new List<Transform>(); void Awake() { for (int i = 0; i < number; i++) { float randomPosX = Random.Range(-50, 50); float randomPosY = Random.Range(7, 100); GameObject child = Instantiate(obj, new Vector3(transform.position.x + randomPosX, 2.5f, transform.position.z + randomPosY), Quaternion.identity); child.transform.SetParent(this.transform); objs.Add(child.transform); } } private void Update() { time += Time.deltaTime; if(time>speed) { for (int i = 0; i < number; i++) { float randomPosX = Random.Range(-50, 50); float randomPosY = Random.Range(7, 100); objs[i].localPosition = new Vector3(transform.position.x + randomPosX, 0, transform.position.z + randomPosY); } time = 0; } for (int i = 0; i < number; i++) { float randomS = Random.Range(.1f, 2f); objs[i].localScale = new Vector3(randomS, randomS, 1); } } } | cs |
'programming > c#' 카테고리의 다른 글
유니티 textureOffset (0) | 2020.02.19 |
---|---|
유니티 line renderer로 그리드 생성 (0) | 2020.02.15 |
유니티 Physics - 움직임의 jitter를 줄이는 방법 (0) | 2020.01.04 |
GPU Instancing 최적화! 최적화! (1) | 2020.01.01 |
GetPixels (0) | 2020.01.01 |