[英]Unity 3D Set object in cone
I'm looking to place an object at a specific position relative to another : 我想将一个物体放置在相对于另一个物体的特定位置:
This new object has to be placed in the pink zone, and I only know the minimum and max distance of placement, an angle relative to my first object forward direction (maxAngle in degrees), and the position of this first object. 这个新对象必须放置在粉红色区域中,我只知道最小和最大放置距离,相对于我的第一个对象向前方向的角度(maxAngle以度为单位)以及该第一个对象的位置。
I already know how to check if an object is placed in the pink zone, but not set its position in this zone. 我已经知道如何检查对象是否位于粉红色区域中,但是没有设置其在该区域中的位置。 So I took the code to check an object in the cone, but I can't get how to transform it to set the position in the cone. 因此,我使用代码检查了圆锥中的对象,但无法获取如何对其进行变换以设置圆锥中的位置的信息。
float distance = Random.Range(minDistance, maxDistance);
float angle = maxAngle *= Mathf.Deg2Rad;
float coneRadius = distance * Mathf.Tan(angle);
Vector3 vect = firstObject.transform.position - targetObject.transform.position;
targetObject.transform.position = new Vector3(angle, 0, firstObject.transform.position.z + distance);
If you can give me clues, it'll be very cool. 如果您能给我一些提示,那就太好了。
This is indeed a basic technique in Unity or any transform-based scene engine. 实际上,这确实是Unity或任何基于变换的场景引擎中的一项基本技术。
Create the new object, "newb". 创建新对象“ newb”。
(1) Position the object exactly at the "+" in your image. (1)将对象精确定位在图像中的“ +”位置。
(2) Choose your angle (2)选择你的角度
angle = Random.Range(-maxAngle, maxAngle);
(3) Twist newb by that much: (3)将newb扭转这么多:
newb.transform.eulerAngles = new Vector3( 0f, 0f, angle);
(4) Choose your distance: (4)选择您的距离:
distance = Random.Range(minDistance,maxDistance);
(5) Then offset the LOCAL position of newb by that much: (5)然后将newb的LOCAL位置偏移那么大:
newb.transform.Translate(0f, 0f, distance, Space.Self);
And then the trick: 然后诀窍:
Note that "newb" will be "twisted", so make it sit straight: 请注意,“ newb”将被“扭曲”,因此请使其坐直:
newb.transform.eulerAngles = Vector3.zero;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.