Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cowcatcherer · Jun 24, 2014 at 05:50 PM · meshcolliderinertiatensor

how to get two script edited mesh colliders to collide.

I know this is asked a lot, but none of the other answers fixed my problem. this is the script of the terrain:

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

public class PolygonGenerator : MonoBehaviour {

 public List<Vector3> newVertices = new List<Vector3>();
 public List<int> newTriangles = new List<int>();
 public List<Vector2> newUV = new List<Vector2>();
 
 public List<Vector3> colVertices = new List<Vector3>();
 public List<int> colTriangles = new List<int>();
 private int colCount;
 
 private Mesh mesh;
 private MeshCollider col;
 
 private float tUnit = 0.25f;
 public Vector2 tStone = new Vector2 (0f,0f);
 public Vector2 tGrass = new Vector2 (0f,.5f);
 
 
 public byte[,] blocks;
 private int squareCount;
 public bool update=false;
 
 
 // Use this for initialization
 void Start () {
     mesh = GetComponent<MeshFilter> ().mesh;
     col = GetComponent<MeshCollider> ();
     
     GenTerrain();
     BuildMesh();
     UpdateMesh();
 }
 
 void Update(){
     if(update){
         BuildMesh();
         UpdateMesh();
         update=false;
     }
 }
 
 
 int NoiseInt (int x, int y, float scale, float mag, float exp){
     
     return (int) (Mathf.Pow ((Mathf.PerlinNoise(x/scale,y/scale)*mag),(exp) ));
     
     
 }
 
 void GenTerrain(){
     blocks=new byte[96,128];
     
     for(int px=0;px<blocks.GetLength(0);px++){
         int stone= NoiseInt(px,0, 80,15,1);
         stone+= NoiseInt(px,0, 50,30,1);
         stone+= NoiseInt(px,0, 10,10,1);
         stone+=75;
         
         
         int dirt = NoiseInt(px,0, 100f,35,1);
         dirt+= NoiseInt(px,100, 50,30,1);
         dirt+=75;
         
         
         for(int py=0;py<blocks.GetLength(1);py++){
             if(py<stone){
                 blocks[px, py]=1;
                 
                 if(NoiseInt(px,py,12,16,1)>10){  //dirt spots
                     blocks[px,py]=2;
                     
                 }
                 
                 if(NoiseInt(px,py*2,16,14,1)>10){ //Caves
                     blocks[px,py]=0;
                     
                 }
                 
             } else if(py<dirt) {
                 blocks[px,py]=2;
             }
             
             
         }
     }
 }
 
 void BuildMesh(){
     for(int px=0;px<blocks.GetLength(0);px++){
         for(int py=0;py<blocks.GetLength(1);py++){
             
             if(blocks[px,py]!=0){
                 
                 GenCollider(px,py);
                 
                 if(blocks[px,py]==1){
                     GenSquare(px,py,tStone);
                 } else if(blocks[px,py]==2){
                     GenSquare(px,py,tGrass);
                 }
             }
         }
     }
 }
 
 byte Block (int x, int y){
     
     if(x==-1 || x==blocks.GetLength(0) || y==-1 || y==blocks.GetLength(1)){
         return (byte)1;
     }
     
     return blocks[x,y];
 }
 
 void GenCollider(int x, int y){
     
     
     //Top
     if(Block(x,y+1)==0){
         colVertices.Add( new Vector3 (x  ,  y  , 1));
         colVertices.Add( new Vector3 (x + 1 ,  y  , 1));
         colVertices.Add( new Vector3 (x + 1 ,  y , 0 ));
         colVertices.Add( new Vector3 (x  ,  y  , 0 ));
         
         ColliderTriangles();
         
         colCount++;
     }
     
     //bot
     if(Block(x,y-1)==0){
         colVertices.Add( new Vector3 (x  ,  y -1 , 0));
         colVertices.Add( new Vector3 (x + 1 ,  y -1 , 0));
         colVertices.Add( new Vector3 (x + 1 ,  y -1 , 1 ));
         colVertices.Add( new Vector3 (x  ,  y -1 , 1 ));
         
         ColliderTriangles();
         colCount++;
     }
     
     //left
     if(Block(x-1,y)==0){
         colVertices.Add( new Vector3 (x  ,  y -1 , 1));
         colVertices.Add( new Vector3 (x  ,  y  , 1));
         colVertices.Add( new Vector3 (x  ,  y  , 0 ));
         colVertices.Add( new Vector3 (x  ,  y -1 , 0 ));
         
         ColliderTriangles();
         
         colCount++;
     }
     
     //right
     if(Block(x+1,y)==0){
         colVertices.Add( new Vector3 (x +1 ,  y  , 1));
         colVertices.Add( new Vector3 (x +1 ,  y -1 , 1));
         colVertices.Add( new Vector3 (x +1 ,  y -1 , 0 ));
         colVertices.Add( new Vector3 (x +1 ,  y  , 0 ));
         
         ColliderTriangles();
         
         colCount++;
     }
     
 }
 
 void ColliderTriangles(){
     colTriangles.Add(colCount*4);
     colTriangles.Add((colCount*4)+1);
     colTriangles.Add((colCount*4)+3);
     colTriangles.Add((colCount*4)+1);
     colTriangles.Add((colCount*4)+2);
     colTriangles.Add((colCount*4)+3);
 }
 
 
 void GenSquare(int x, int y, Vector2 texture){
     
     newVertices.Add( new Vector3 (x  ,  y  , 0 ));
     newVertices.Add( new Vector3 (x + 1 ,  y  , 0 ));
     newVertices.Add( new Vector3 (x + 1 ,  y-1 , 0 ));
     newVertices.Add( new Vector3 (x  ,  y-1 , 0 ));
     
     newTriangles.Add(squareCount*4);
     newTriangles.Add((squareCount*4)+1);
     newTriangles.Add((squareCount*4)+3);
     newTriangles.Add((squareCount*4)+1);
     newTriangles.Add((squareCount*4)+2);
     newTriangles.Add((squareCount*4)+3);
     
     newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
     newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
     newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
     newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y));
     
     squareCount++;
     
 }
 
 void UpdateMesh () {
     mesh.Clear ();
     mesh.vertices = newVertices.ToArray();
     mesh.triangles = newTriangles.ToArray();
     mesh.uv = newUV.ToArray();
     mesh.Optimize ();
     mesh.RecalculateNormals ();
     
     newVertices.Clear();
     newTriangles.Clear();
     newUV.Clear();
     squareCount=0;
     
     Mesh newMesh = new Mesh();
     newMesh.vertices = colVertices.ToArray();
     newMesh.triangles = colTriangles.ToArray();
     col.sharedMesh= newMesh;
     
     colVertices.Clear();
     colTriangles.Clear();
     colCount=0;
 }

} and this is the script for the object that needs to collide with the terrain:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class vehicle : MonoBehaviour {
     
     public List<Vector3> newVertices = new List<Vector3>();
     public List<int> newTriangles = new List<int>();
     public List<Vector2> newUV = new List<Vector2>();
     
     public List<Vector3> colVertices = new List<Vector3>();
     public List<int> colTriangles = new List<int>();
     private int colCount;
     
     private Mesh mesh;
     private MeshCollider col;
     
     private float tUnit = 0.25f;
     public Vector2 tStone = new Vector2 (0f,0f);
     public Vector2 tGrass = new Vector2 (0f,.5f);
     
     
     public byte[,] blocks;
     private int squareCount;
     public bool update=false;
     
     
     // Use this for initialization
     void Start () {
         mesh = GetComponent<MeshFilter> ().mesh;
         col = GetComponent<MeshCollider> ();
         
         GenTerrain();
         BuildMesh();
         UpdateMesh();
 
         rigidbody.inertiaTensor = Vector3.one;
         rigidbody.inertiaTensorRotation = Quaternion.identity;
     }
     
     void Update(){
         if(update){
             BuildMesh();
             UpdateMesh();
             update=false;
         }
     }
     
     
     int NoiseInt (int x, int y, float scale, float mag, float exp){
         
         return (int) (Mathf.Pow ((Mathf.PerlinNoise(x/scale,y/scale)*mag),(exp) ));
         
         
     }
     
     void GenTerrain(){
         blocks=new byte[1,1];
         
         for(int px=0;px<blocks.GetLength(0);px++){
             int stone= NoiseInt(px,0, 80,15,1);
             stone+= NoiseInt(px,0, 50,30,1);
             stone+= NoiseInt(px,0, 10,10,1);
             stone+=75;
             
             
             int dirt = NoiseInt(px,0, 100f,35,1);
             dirt+= NoiseInt(px,100, 50,30,1);
             dirt+=75;
             
             
             for(int py=0;py<blocks.GetLength(1);py++){
                 if(py<stone){
                     blocks[px, py]=1;
                     
                     if(NoiseInt(px,py,12,16,1)>10){  //dirt spots
                         blocks[px,py]=2;
                         
                     }
                     
                     if(NoiseInt(px,py*2,16,14,1)>10){ //Caves
                         blocks[px,py]=0;
                         
                     }
                     
                 } else if(py<dirt) {
                     blocks[px,py]=2;
                 }
                 
                 
             }
         }
     }
     
     void BuildMesh(){
         for(int px=0;px<blocks.GetLength(0);px++){
             for(int py=0;py<blocks.GetLength(1);py++){
                 
                 if(blocks[px,py]!=0){
                     
                     GenCollider(px,py);
                     
                     if(blocks[px,py]==1){
                         GenSquare(px,py,tStone);
                     } else if(blocks[px,py]==2){
                         GenSquare(px,py,tGrass);
                     }
                 }
             }
         }
     }
     
     byte Block (int x, int y){
         
         if(x==-1 || x==blocks.GetLength(0) || y==-1 || y==blocks.GetLength(1)){
             return (byte)1;
         }
         
         return blocks[x,y];
     }
     
     void GenCollider(int x, int y){
         
     
         //Top
         if(Block(x,y+1) ==1){
             colVertices.Add( new Vector3 (x  ,  y  , 1));
             colVertices.Add( new Vector3 (x + 1 ,  y  , 1));
             colVertices.Add( new Vector3 (x + 1 ,  y , 0 ));
             colVertices.Add( new Vector3 (x  ,  y  , 0 ));
             colVertices.Add( new Vector3 (x  ,  y-1  , 1));
             colVertices.Add( new Vector3 (x + 1 ,  y-1  , 1));
             colVertices.Add( new Vector3 (x + 1 ,  y-1 , 0 ));
             colVertices.Add( new Vector3 (x  ,  y-1  , 0 ));
 
             ColliderTriangles();
             
             colCount++;
         }
         
         //bot
         if(Block(x,y-1)==0){
             colVertices.Add( new Vector3 (x  ,  y -1 , 0));
             colVertices.Add( new Vector3 (x + 1 ,  y -1 , 0));
             colVertices.Add( new Vector3 (x + 1 ,  y -1 , 1 ));
             colVertices.Add( new Vector3 (x  ,  y -1 , 1 ));
             
             ColliderTriangles();
             colCount++;
         }
         
         //left
         if(Block(x-1,y)==0){
             colVertices.Add( new Vector3 (x  ,  y -1 , 1));
             colVertices.Add( new Vector3 (x  ,  y  , 1));
             colVertices.Add( new Vector3 (x  ,  y  , 0 ));
             colVertices.Add( new Vector3 (x  ,  y -1 , 0 ));
             
             ColliderTriangles();
             
             colCount++;
         }
         
         //right
         if(Block(x+1,y)==0){
             colVertices.Add( new Vector3 (x +1 ,  y  , 1));
             colVertices.Add( new Vector3 (x +1 ,  y -1 , 1));
             colVertices.Add( new Vector3 (x +1 ,  y -1 , 0 ));
             colVertices.Add( new Vector3 (x +1 ,  y  , 0 ));
             
             ColliderTriangles();
             
             colCount++;
         }
         
     }
     
     void ColliderTriangles(){
         colTriangles.Add(colCount*4);
         colTriangles.Add((colCount*4)+1);
         colTriangles.Add((colCount*4)+3);
 
         colTriangles.Add((colCount*4)+1);
         colTriangles.Add((colCount*4)+2);
         colTriangles.Add((colCount*4)+3);
 
         colTriangles.Add((colCount*4)+0);
         colTriangles.Add((colCount*4)+3);
         colTriangles.Add((colCount*4)+7);
 
         colTriangles.Add((colCount*4)+0);
         colTriangles.Add((colCount*4)+4);
         colTriangles.Add((colCount*4)+7);
 
         colTriangles.Add((colCount*4)+4);
         colTriangles.Add((colCount*4)+6);
         colTriangles.Add((colCount*4)+7);
 
         colTriangles.Add((colCount*4)+4);
         colTriangles.Add((colCount*4)+5);
         colTriangles.Add((colCount*4)+6);
 
         colTriangles.Add((colCount*4)+0);
         colTriangles.Add((colCount*4)+4);
         colTriangles.Add((colCount*4)+1);
 
         colTriangles.Add((colCount*4)+4);
         colTriangles.Add((colCount*4)+5);
         colTriangles.Add((colCount*4)+1);
 
         colTriangles.Add((colCount*4)+5);
         colTriangles.Add((colCount*4)+6);
         colTriangles.Add((colCount*4)+2);
 
         colTriangles.Add((colCount*4)+5);
         colTriangles.Add((colCount*4)+1);
         colTriangles.Add((colCount*4)+2);
 
         colTriangles.Add((colCount*4)+6);
         colTriangles.Add((colCount*4)+3);
         colTriangles.Add((colCount*4)+2);
 
         colTriangles.Add((colCount*4)+6);
         colTriangles.Add((colCount*4)+3);
         colTriangles.Add((colCount*4)+7);
 
     }
     
     
     void GenSquare(int x, int y, Vector2 texture){
         
         newVertices.Add( new Vector3 (x  ,  y  , 0 ));
         newVertices.Add( new Vector3 (x + 1 ,  y  , 0 ));
         newVertices.Add( new Vector3 (x + 1 ,  y-1 , 0 ));
         newVertices.Add( new Vector3 (x  ,  y-1 , 0 ));
         
         newTriangles.Add(squareCount*4);
         newTriangles.Add((squareCount*4)+1);
         newTriangles.Add((squareCount*4)+3);
         newTriangles.Add((squareCount*4)+1);
         newTriangles.Add((squareCount*4)+2);
         newTriangles.Add((squareCount*4)+3);
         
         newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
         newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
         newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
         newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y));
         
         squareCount++;
         
     }
     
     void UpdateMesh () {
         mesh.Clear ();
         mesh.vertices = newVertices.ToArray();
         mesh.triangles = newTriangles.ToArray();
         mesh.uv = newUV.ToArray();
         mesh.Optimize ();
         mesh.RecalculateNormals ();
         
         newVertices.Clear();
         newTriangles.Clear();
         newUV.Clear();
         squareCount=0;
         
         Mesh newMesh = new Mesh();
         newMesh.vertices = colVertices.ToArray();
         newMesh.triangles = colTriangles.ToArray();
         col.sharedMesh= newMesh;
         
         colVertices.Clear();
         colTriangles.Clear();
         colCount=0;
     }
 }

Every time i run the game, it gives me the error: Actor::updateMassFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually!

and:

Actor::updateMassFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually! UnityEngine.MeshCollider:set_sharedMesh(Mesh) vehicle:UpdateMesh() (at Assets/scripts/vehicle.cs:269) vehicle:Start() (at Assets/scripts/vehicle.cs:35)

this is important since its the core feature of the game.

edit: I did try making the colliders for each box a rectangular prism, but it didn't fix the error:(

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Wierd Internal Physics problem 1 Answer

Compute the torque needed to rotate an object at a constant rate 3 Answers

Excluding a collider from tensor calculations? 0 Answers

inertiaTensor stopping automatic rotation 1 Answer

How do I programmatically Combine Inertia Tensors? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges