- Home /
[Closed]Help with Script for Terrain deforming
I hav this Javascript Script:
private var tData : TerrainData;
private var saved : float[,];
public var explosion : GameObject;
var countdown :float;
var cratertex : Texture2D;
var xRes;
var yRes;
var craterData;
var xkoordinate :int;
var zkoordinate :int;
var explkoordinate :Vector3;
public var Spieler :GameObject;
function Start () {
countdown=1;
explkoordinate= new Vector3(0,0,0);
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 Update ()
{ countdown-=Time.deltaTime;
if (countdown<0&&Spieler!=null)
{
zkoordinate = Random.Range(Spieler.transform.position.z, Spieler.transform.position.z);
xkoordinate= Random.Range(Spieler.transform.position.x, Spieler.transform.position.x);
var x : int = Mathf.Lerp(0, xRes, Mathf.InverseLerp(0, tData.size.x,Spieler.transform.position.x));
var z : int = Mathf.Lerp(0, yRes, Mathf.InverseLerp(0, tData.size.z,Spieler.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);
explkoordinate= new Vector3(xkoordinate,cratertex.height+10,zkoordinate);
Instantiate (explosion, explkoordinate, Quaternion.identity);
countdown =1;
}
}
It will make a Pan every Second in the Terrain where the Player stands and instantiates an Explosion.Everxthing works,but if i make the terrain bigger, also the pan gets bigger.How do i make it that the pan does not get bigger with bigger Terrain?
Sorry for my English.
Answer by CScherer549 · Jul 20, 2014 at 08:11 PM
My guess is that because you're setting you xRes and yRes variables from the heightmap(which is being generated from the terrain?) that's causing your numbers here:
x = Mathf.Clamp(x, cratertex.width/2, xRes-cratertex.width/2); z = Mathf.Clamp(z, cratertex.height/2, yRes-cratertex.height/2);
to change with the scale/size of the terrain. That's my best guess from the info provided anyway.
What additional information do you need and what do i need to change in my script, that the pan stops changing his size.
A couple things.
What is a Pan?
Are you changing the terrain size after you start the game/after the script has run?
If you are changing the size after then the problem might be that scaling terrain doesn't make more, it just expands the original. This could cause all the data on the terrain to remain the same while it appears bigger.
Otherwise, I think the problem is that you're getting values (your xRes and yRes) from terrain data and using those to calculate the size/area of your explosion. This terrain data is pulled from your terrain, so it will always be relative to the terrain data, in this case the size.
Hopefully this helps, let me know if you need some clarification on anything!
Pan is a Crater
change the Terrain size in the editor befor running the game/script
am changin the Terrain width and lenght from 300 to 3000, but if i change then the script to
xRes = tData.heightmapWidth/10;
i still get a giant crater and if i doxRes = tData.heightmapWidth*10;
i get following errors wile running the game:
1.NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
2.NullReferenceException: A null value was found where an object instance was required. UnityEngine.TerrainData.SetHeights (Int32 xBase, Int32 yBase, System.Single[,] heights) (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/TerrainDataBindings.cs:239) cratermaker.OnApplicationQuit () (at Assets/Standard Assets/cratermaker.js:27)
the last error is about this line :tData.SetHeights(0,0,saved);
I couldn't get the script to run on my end, but my advice would be to throw some debug statements in at places where values are changing. It could be that one of the functions you have in there, like a clamp or your lerps isn't giving you the values you want.
One interesting thing I found by messing around with terrains is that the height map size doesn't change unless you set it, which should mean (unless you're changing it) your xRes and yRes values are constant.
the first error is because your tData is co$$anonymous$$g back null for some reason (my guess anyway), also what line was this error being called on? The second error is because you're then asking for a height not in the height map.
Without your entire code base my best guess is that you're using some dynamic variable to grab terrain points to deform and this variable is changing with the size of your terrain. I would mess around with your basic terrain and see if you can change the size of the crater using just your script. From there it shouldn't be too hard to make it a constant size.
Hope that helped, if not feel free to ask more questions.
I took again a close look at the script and noticed, that while i was scaling the Terrain widh and lengh in the terrain settings from 300 to 3000 .But xres and yres is about the heighmap, so i tried to scale the heighmap resolution from 129 to 1290(because its the same factor in width and lengh) but as soon as i tried to confirm the change it autoscaled the value to 2049(don't why) and know it works. I will mark your answer as right so people can find my comment and the resolution, thanks for helping me over days.
PS: Excuse my English.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Is Terrain Glitched???? 0 Answers
My characters can't sto falling terrain 1 Answer
Terrain Texture Glitch? 1 Answer
Can you stream terrain with Unity Indie? 3 Answers