首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从三维建模软件中获取导入的游戏对象的中心

从三维建模软件中获取导入的游戏对象的中心
EN

Stack Overflow用户
提问于 2016-03-09 13:06:38
回答 1查看 93关注 0票数 0

我是新来的,我是一个团结的初学者。对于我的游戏,我导入了一个戒指,我创建了一个3建模软件。现在我想找出那个戒指的中心。此外,我想跟踪这个环的中心位置,并把它放在一个向量中。请帮帮忙。我该怎么做?

谢谢!

编辑:

谢谢你的回答,很抱歉我说得不够具体。为了更清楚地知道我想尝试什么,我给你一些我想做的事情的信息。我会创造一个“热电线游戏”与正弦波移动电线,必须跟着一个戒指。这个游戏的目的是,环的中心必须在电线上。因此,如果玩家不够精确,就会有一个错误(期望的位置是移动的电线-实际位置,它将是圆环的中心)。

下面是我最后想要的内容的链接:

https://itunes.apple.com/de/app/hot-wire-heisser-draht/id941657787?mt=8

https://processing.org/examples/sinewave.html

到目前为止,我已经得到了由多个球构成的运动正弦波的编码。在这里,代码:

代码语言:javascript
复制
using UnityEngine;
using System.Collections;

public class SineWaveSpheres : MonoBehaviour {

    public GameObject plotPointObject;  public int numberOfPoints= 100;
    private float animSpeed =1.0f;  public float scaleInputRange = 8*Mathf.PI; // scale number from [0 to 99] to [0 to 2Pi] //Zahl vor Mathf, Anzahl Bön
    public float scaleResult = 2.5f; // Y Achse Range
    public bool animate = true;
    public Vector3 posSphere;
    GameObject[] plotPoints;

    // Use this for initialization
    void Start () {
        if (plotPointObject == null) //if user did not fill in a game object to use for the plot points
            plotPointObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); //create a sphere

        //add Material to the spheres , load material in the folder Resources/Materials
        Material myMaterial = Resources.Load("Materials/green", typeof(Material)) as Material;
        plotPointObject.GetComponent<MeshRenderer> ().material = myMaterial;

        //change the scale of the spheres 
        plotPointObject.transform.localScale = Vector3.one * 0.5f ;

        plotPoints = new GameObject[numberOfPoints]; //creat an array of 100 points.
        //plotPointObject.GetComponent<MeshRenderer>().material =Material.Load("blue") as Material

        //plotPointObject.transform.localScale -= new Vector3 (0.5F, 0.5F, 0.5F); //neu: change the scale of the spheres

        for (int i = 0; i < numberOfPoints; i++)
        {
            plotPoints[i] = (GameObject)GameObject.Instantiate(plotPointObject, new Vector3(i - (numberOfPoints/2), 0, 0), Quaternion.identity); //this specifies what object to create, where to place it and how to orient it
        }       //we now have an array of 100 points- your should see them in the hierarchy when you hit play

        plotPointObject.SetActive(false); //hide the original
    }

    // Update is called once per frame
    void Update() {
        for (int i = 0; i < numberOfPoints; i++) 
        {
            float functionXvalue = i * scaleInputRange / numberOfPoints; // scale number from [0 to 99] to [0 to 2Pi]
            if (animate) {
                functionXvalue += Time.time * animSpeed;
            }

            plotPoints[i].transform.position = new Vector3(i - (numberOfPoints/2), ComputeFunction(functionXvalue) * scaleResult, 0); 

            // put the position information of sphere clone 50 in a vector3 named posSphere posSphere = plotPoints [50].transform.position;
        }
        //print position of sphere 50 in console        //print (posSphere);
    }

    float ComputeFunction(float x)
    {
        return Mathf.Sin(x);
    }
}

就像我说的,我有一个3D建模软件的戒指。环的中心似乎是原点(0,0,0)。因此,我的想法是,我可以执行以下操作来计算错误:

代码语言:javascript
复制
error=posSphere-posRing 

在posSphere中,我会从上面的SineWave脚本中获取,而posRing则会从环形脚本中获取

代码语言:javascript
复制
posRing=transform.position;

我错了吗?

我的问题:

  1. 我必须在哪里(在哪个脚本中)做这个计算?在哪个函数中(LateUpdate,FixedUpdate)?
  2. 我试图通过以下方式访问环形脚本中的向量posSphere 误差=正弦波球.偏置球-偏置环; 其中,SineWaveSpheres是正弦波移动球脚本的类。这个逻辑正确吗?
EN

回答 1

Stack Overflow用户

发布于 2016-03-09 22:28:54

好吧,这有很多要解释的。首先,您有一个很好的代码,但它与您的问题无关,对吗?但无论如何,这是IMHO的答案

  1. 因为这个脚本只是为了创建正弦波,所以您应该为圆环的内容(运动、逻辑、数学等)创建另一个脚本。在这里,您可以在Update函数中进行计算(当您想要覆盖动画转换时,可以使用LateUpdate;在引用受物理影响的GameObjects时使用FixedUpdate )。无论如何,我强烈建议你避免做矢量校正,只需修改你的3D套件中的环的中心。
  2. 这主要是因为您正在通过类本身访问非静态成员,因此无法工作;您必须使用类的一个实例。有点像 var sineWaveSpheres = someGameObject.GetComponent ();

问题是,我不建议你这样检查玩家是否失败,我的意思是,你可以做一次碰撞检查:

代码语言:javascript
复制
void OnCollisionEnter (Collision col)
{
    if (col.gameObject.name == "the name of the sine wave spehere gameobject")
    {
        //game over, do stuff
    }
}

必须将此代码添加到附加到环GameObject的脚本中。

这样你就不用担心环的中心和矢量的数学问题了。唯一要记住的是,你必须在圆环和球体预置中加入一个对撞机才能被实例化。

我就是这么想的,希望能帮上忙!^^

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35892478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档