Generate an object based on angle and distance in Unity, and generate an object based on Unity distance.
Using UnityEngine; using System. collections; public class CreateGameObject: MonoBehaviour {private Vector3 centerPos; // The position of the center private float radius = 5; // The radius private float angle = 0; // The angle public GameObject cube; // Use this for initialization void Start () {CreateMosquitoCoil ();} public void CreateCubeAngle30 () {centerPos = transform. position; // every 30 degrees, a small square for (angle = 0; angle <360; angle + = 30) is generated {// The position of the object is obtained based on the origin, angle, and radius. // x = Origin x + radius * adjacent edge divided by oblique edge ratio, adjacent edge divided by oblique edge ratio = cos (radian), radian = angle * 3.14f/180f; float x = centerPos. x + radius * Mathf. cos (angle * 3.14f/180f); float y = centerPos. y + radius * Mathf. sin (angle * 3.14f/180f); // generate a square GameObject obj = (GameObject) GameObject. instantiate (cube, new Vector3 (x, y, centerPos. z), Quaternion. identity) ;}}// generate mosquito repellent incense public void CreateMosquitoCoil () {centerPos = transform. position; // a small square for (int I = 0; I <30; angle + = 20, radius + = 0.3f, I ++) is generated every 30 degrees) {// obtain the Object Location Based on the origin, angle, and radius. x = Origin x + radius * adjacent edge divided by oblique edge ratio float x = centerPos. x + radius * Mathf. cos (angle * 3.14f/180f); float y = centerPos. y + radius * Mathf. sin (angle * 3.14f/180f); // generate a square GameObject obj = (GameObject) GameObject. instantiate (cube, new Vector3 (x, y, centerPos. z), Quaternion. identity );}}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.