Generate an object based on angle and distance in Unity, and generate an object based on Unity distance.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.