游戏对象在与地形接触时振动

Tol*_*ure 5 c# unity-game-engine

首先,我宁愿知道为什么会发生这种情况,然后再进行修复。知道可以让我学习,而剪切和粘贴代码则不行。(另一种解决方法会非常好,我谢谢你,但这让我感到困惑,我想知道为什么)。

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

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

我希望有人知道为什么会这样。如果有帮助,我会扩展 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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

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