- Home /
DDT script only works on one terrain
Hello Everyone,
I have a dynamic destructible terrain script that works perfectly one terrain I have (the only one in the scene). The purpose of it is to make craters upon collision with my projectile. Unfortunately, when I apply the script to a different terrain inside a different scene, the script fails to work! It is very annoying, as I have fiddled around with the code as much as I could and have not been able to solve this issue. Code:
private var tData : TerrainData;
private var saved : float[,];
var cratertex : Texture2D;
var xRes;
var yRes;
var craterData;
function Start () {
tData = Terrain.activeTerrain.terrainData;
xRes = tData.heightmapWidth;
yRes = tData.heightmapHeight;
saved = tData.GetHeights(0,0,xRes,yRes);
craterData = cratertex.GetPixels();
}
function OnApplicationQuit () {
tData.SetHeights(0,0,saved);
}
function OnCollisionEnter (hit:Collision)
{
if (hit.gameObject.tag=="projectile")
{
var x : int = Mathf.Lerp(0, xRes, Mathf.InverseLerp(0, tData.size.x, GameObject.FindWithTag("projectile").transform.position.x));
var z : int = Mathf.Lerp(0, yRes, Mathf.InverseLerp(0, tData.size.z, GameObject.FindWithTag("projectile").transform.position.z));
x = Mathf.Clamp(x, cratertex.width/2, xRes-cratertex.width/2);
z = Mathf.Clamp(z, cratertex.height/2, yRes-cratertex.height/2);
var areaT = tData.GetHeights(x-cratertex.width/2, z-cratertex.height/2, cratertex.width, cratertex.height);
for (i = 0; i < cratertex.height; i++) {
for (j = 0; j < cratertex.width; j++) {
areaT [i,j] = areaT [i,j] - craterData[i*cratertex.width+j].a*0.01;
}
}
tData.SetHeights(x-cratertex.width/2, z-cratertex.height/2, areaT);
}
}
I appreciate your help, in any coding language except Boo. -Hyperion
Is the new terrain as low as it can get? Click it and see if you can manually lower it.
I can move it, I can do whatever with it, except I cannot change its size.
And the size is too small for my liking, so that's why I want to apply it on a separate terrain that is much larger.
Are you saying it works on any terrain provided the size is the same?
Answer by meat5000 · Aug 31, 2013 at 02:08 AM
Erm DUDE. You are going to hate this :P
I deleted the terrain in the project and stuck my own in to it. I put the same scripts on it as the example and put the same figures in and...well it just worked. The only thing that was different was the actual crater texture which just came out black, but the deforming worked and everything. But just as I said in my first comment.... the original terrain had lots of height. A generated terrain has very little height.
I changed:
Terrain Height, Terrain Width, Terrain Height, Heightmap resolution, Detail resolution,
And it still worked.
EDIT: I changed everything in the terrain. It all just worked.
So, my answer, Make sure your scripts are in the right order and the numbers are the same (128,128,2). Also make sure Splat Alpha 0 is in the correct place! Literally copy the example! But most importantly make sure your terrain is not at the lowest height!
Wow, thank you very much! I will try it tomorrow so I can accept it! Just wondering, your new terrain was much larger than the old one, right? What order are your scripts in? And where do I make sure splat alpha 0 is in the right place?
I even increased the terrain size to 4000x4000!
Under terrain collider comes Cratermaker, then TextureChanger. I had manually put figures in to TextureChanger but I didn't even notice that it fills them in when you Play.
Ok Under Assets There is a folder called 'Terrain $$anonymous$$orpher'. In this folder are 4 js scripts. Cratermaker, Cursor, $$anonymous$$ouse, Texturechanger.
Under Assets, not in a folder, TerrainChanger.js and tData. tData has SplatAlpha 0 in it.
There is a folder called Detonator, but I don't think I've used anything in it.
O$$anonymous$$AY, thank you so much! Just as you said, I had to go into the terrain editor thing and manually raise the height. Before, I thought I had to change the position. What's strange is that the holes made are HUGE, unlike the original deformations.
The must have been scaled as a factor of the terrain size :)
Is the new terrain as low as it can get? Click it and see if you can manually lower it.
Lol, my first comment ;P
Lol, if only you had said raise it ins$$anonymous$$d. Thanks for your input!
Your answer
![](https://koobas.hobune.stream/wayback/20220613112915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Camera not through terrain 3 Answers
Collision with Terrain 2 Answers
How do i test whether my box collider is touching the floor or not? 2 Answers