GameObjects在与地形接触时会振动

时间:2014-05-31 00:37:49

标签: c# unity3d

首先,我宁愿知道为什么会这样,然后得到修复。知道将允许我学习,而剪切和粘贴代码不会。 (一个替代修复将是非常好的,我感谢你,但这让我感到困惑,我想知道为什么)。

设置: 我有一个原始形状GO(gameobject)在地形之上产卵。这个GO有一个僵硬的身体,我附上一个脚本。 Seed.cs这个类从另一个名为body的类继承了几个函数。一旦产生,GO就会下降并与地形接触,这就是问题发生的地方。 GO可以穿过地形,卡在里面一半,跌落卡住然后被抛出地面,或者如果跌落足够慢只是持续振动。

我尝试在GO上运行场景之前将碰撞检测更改为所有三种不同类型,但影响不大。我找到的工作是在场景运行时我可以将碰撞检测从一个选项更改为另一个选项,我的问题似乎消失了。我可以在Body类的start函数中抛出此更改,它可以正常工作。碰撞检测的实际选项与解决问题无关,只需要我更改它。从连续到离散或离散到连续动态只有我改变它才能解决问题的事实。

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

代码可能看起来像意大利面,因为这是一个娱乐节目,这也是我第一次使用Unity。

贝娄是我的代码。 SimulationController

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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

听起来我觉得物品被拉得太快,嵌入自己或完全穿过地形。由于地形基本上是2d,所以它的速度并不像你想象的那么快。

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

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