以统一3D实例化2个对象之间的游戏对象 [英] Instantiate gameobject between 2 objects in unity 3d

查看:88
本文介绍了以统一3D实例化2个对象之间的游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个对象.它会在不同的方向和距离.

I have 2 objects. It will be in various direction and distance.

我如何实例化它们之间具有特定距离的对象.

How can i instantiate objects between them with a specific distance.

推荐答案

我的建议是像这样计算两个对象之间的向量

My suggestion would be to calculate the vector between the two objects, like this

Vector3 objectLine = (object2.transform.position - object1.transform.position);

存储该向量的大小

float distance = objectLine.magnitude;

然后,对向量进行归一化;

Then, normalise the vector;

objectLine = objectLine.normalized;

遍历该行,实例化您要创建特定距离的对象

Iterate through the line, instanciating the object you want to create a specific distances

Vector3 creationPoint = object1.transform.position;
float creationPointDistance = (object1.transform.position - 
    object1.transform.position);
while(creationPointDistance < distance)
{
    creationPoint += objectLine * NEW_OBJECT_DISTANCE;
    creationPointDistance = (object1.transform.position - 
        object1.transform.position);
    if(creationPointDistance < distance)
    {
        objects.Add((GameObject)Instanciate(newObject, creationPoint, 
            new Vector3(0.0f,0.0f,0.0f)));
    }
}

将要执行的操作是将初始点设置为object1的位置.然后,它将沿着对象1和对象2之间的向量移动设定距离,检查它是否在两个对象中,如果是,则实例化该对象,并将其存储在游戏对象列表中.

What that will do is set the initial point to be object1's position. It will then move a set distance along the vector between object 1 and object 2, check it's within the two objects, and if it is, instanciate the object, storing it in a list of gameobjects.

希望可以做到这一点.我面前没有Unity(或任何IDE)来检查语法.

That hopefully should do it. I don't have Unity (or any IDE) in front of me to check the syntax.

这篇关于以统一3D实例化2个对象之间的游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆