【问题标题】:GameObjects vibrate when in contact with terrain游戏对象与地形接触时会振动
【发布时间】:2014-07-20 20:15:46
【问题描述】:

首先我宁愿知道为什么会发生这种情况,然后得到修复。知道会让我学习,而剪切和粘贴代码则不会。 (替代修复会非常好,我感谢你,但这让我感到困惑,我想知道为什么)。

设置: 我在地形上方生成了一个原始形状 GO(游戏对象)。这个 GO 有一个刚体,我在上面附加了一个脚本。 Seed.cs 这个类从另一个名为 body 的类继承了几个函数。一旦生成,GO 就会掉落并与地形接触,这就是问题发生的地方。 GO 可以穿过地形,卡在半个里面,跌倒卡住然后被扔出地面,或者如果跌落速度足够慢,只需持续振动即可。

在 GO 上运行场景之前,我尝试将碰撞检测更改为所有三种不同类型,但效果不大。我发现确实有效的是,当场景运行时,我可以将碰撞检测从一个选项更改为另一个选项,我的问题似乎消失了。我可以在 Body 类的 start 函数中进行这个更改,它工作正常。碰撞检测的实际选项与解决问题无关,它只需要我更改它。从 Continuous 到 Discrete 或 Discrete 到 ContinuousDynamic 唯一的事实是我改变了它,这使得问题消失了。

我希望有人知道为什么会这样。如果有帮助,我会扩展 GO。

代码可能看起来像意大利面条,因为这是一个休闲编程,这也是我第一次使用 Unity。

下面是我的代码。 模拟控制器

public class SimulationControler : MonoBehaviour {

    public GameObject entities;

    // Use this for initialization
    void Start () {
        entities = GameObject.Find ("Enteties");

        EntityHandler entityHandler = entities.GetComponent<EntityHandler>();
        int id = entityHandler.CreateNewEntity ();

        GameObject Adam = entityHandler.GetEntetity (id);
        Entity AdamEntity = Adam.GetComponent<Entity>();
        AdamEntity.StoreResources (1000, 1000);

        GameObject go = (GameObject)Instantiate(Resources.Load("Body Shapes/Cube", typeof(GameObject)), new Vector3(9, 6, 20), Quaternion.Euler(43, 30, 0));
        go.name = "Apple Tree";
        go.rigidbody.angularDrag = 2;
        go.rigidbody.drag = 2;
        go.rigidbody.mass = 444;
        go.AddComponent<Seed> ();
        Seed goSeed = go.GetComponent<Seed> ();
        goSeed.SetDNA("33493339411953125429875244");
        goSeed.SetEntityID (id);
        goSeed.Growth();
        goSeed.Growth();
        goSeed.Growth();
        goSeed.Growth();

    }

    // Update is called once per frame
    void Update () {

    }
}


public class Seed : Body {
}

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class Body : MonoBehaviour {

    private int timet = 0;

    public int maxtimet = 5;
    public string DNA;
    public int DNAPosition = 0;
    public bool Growing = true;
    public int EntityID;

    // Use this for initialization
    void Start () {
        this.rigidbody.collisionDetectionMode  = CollisionDetectionMode.ContinuousDynamic;
    }

    public void SetEntityID (int id)
    {
        EntityID = id;
    }

    public void SetDNA(string sDNA)
    {
        this.DNA = sDNA;
    }

    int DNAReader(int Size){

        string tempString = "";
        for (long i = 0; i < Size; i++)
        {
            //Debug.Log(DNA.Length);
            if(DNA.Length>DNAPosition)
            {
                tempString = tempString + this.DNA[DNAPosition];
                DNAPosition += 1;
            }
        }
        if (tempString == "") 
        {
            tempString = "-1";
        }
        Debug.Log (tempString);
        return Int32.Parse(tempString);

    }


    public void Growth()
    {

        GameObject entities = GameObject.Find ("Enteties");;
        EntityHandler EntityHandler = entities.GetComponent<EntityHandler> ();
        GameObject goEntity = EntityHandler.GetEntetity (EntityID);
        Entity entity = goEntity.GetComponent<Entity>();

        int growth;
        Vector3 Scale;
        int DNACode = 0;
        bool found = false;
        while (!found) 
        {
            int Code = DNAReader (3);
            switch (Code) {
            case 111:
                //Debug.Log ("Go Back");
                DNAPosition -= DNAReader (2);
                if(DNAPosition<0)
                {
                    DNAPosition = 0;
                }
                found = true;
                break;

            case 222:
                //Debug.Log ("Go Forward");
                DNAPosition += DNAReader (2);
                if(DNAPosition>this.DNA.Length)
                {
                    this.Growing = false;
                }
                found = true;
                break;

            case 331:
                growth = DNAReader(1);
                bool temp = entity.RequestResources(growth*2,growth);
                if(entity.RequestResources(growth*2,growth))
                {
                    this.rigidbody.mass += growth;
                    Scale = this.transform.localScale;
                    Scale.x += growth;
                    this.transform.localScale = Scale;
                }

                found = true;
                break;

            case 332:
                growth = DNAReader(1);
                if(entity.RequestResources(growth*2,growth))
                {
                    this.rigidbody.mass += growth;
                    Scale = this.transform.localScale;
                    Scale.y += growth;
                    this.transform.localScale = Scale;
                }

                found = true;
                break;

            case 333:
                growth = DNAReader(1);
                if(entity.RequestResources(growth*2,growth))
                {
                    this.rigidbody.mass += growth;
                    Scale = this.transform.localScale;
                    Scale.z += growth;
                    this.transform.localScale = Scale;
                }

                found = true;
                break;

            case 334:
                int growthx = DNAReader(1);
                int growthy = DNAReader(1);
                int growthz = DNAReader(1);

                growth = growthz*growthy*growthx;
                this.rigidbody.mass += growth;

                if(entity.RequestResources(growth*2,growth))
                {

                    Scale = transform.localScale;


                    Scale.x += growthx; 
                    Scale.y += growthy; 
                    Scale.z += growthz; 

                    transform.localScale = Scale;
                }

                found = true;
                break;


            case 335:
                Scale = this.transform.localScale;

                growth = DNAReader(1);
                Scale.x = growth; 
                Scale.y = growth; 
                Scale.z = growth; 
                this.transform.localScale = Scale;

                found = true;
                break;

            case 411:
                //Create Body part
                int shape = DNAReader(1);
                int type = DNAReader(1);

                int px = DNAReader(1);
                int py = DNAReader(1);
                int pz = DNAReader(1);

                int rx = DNAReader(1);
                int ry = DNAReader(1);
                int rz = DNAReader(1);

                int bf = DNAReader(1);
                int bt = DNAReader(1);
                break;

            case -1:
                this.Growing = false;
                found = true;
                break;
            }
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您是否尝试过检查对象的对撞机以确保它完全适合您的对象。

    【讨论】:

    • 我只使用原始形状,我对它们所做的唯一修改是缩放、旋转和变换(抱歉,如果我使用了错误的语言)。话虽这么说,我还需要检查对撞机吗?
    【解决方案2】:

    在我看来,这件物品被拉得太快并嵌入自身或完全穿过地形。由于地形本质上是二维的,因此通过它并不需要您想象的那么快。

    对撞机的底部是什么形状?我认识到其他问题,但我没有看到您之前描述的“振动”效应。

    既然您提到了缩放,请注意缩放对象会以多种方式对引擎造成严重破坏,但如果我们只讨论正在缩放的​​主要游戏对象(而不是数百个对象或地形本身) ) 这可能不是问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      相关资源
      最近更新 更多