Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TCD · Jul 19, 2014 at 12:44 PM · terrainpan

[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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
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
avatar image TCD · Jul 20, 2014 at 11:07 PM 0
Share

What additional information do you need and what do i need to change in my script, that the pan stops changing his size.

avatar image CScherer549 · Jul 22, 2014 at 12:06 AM 0
Share

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!

avatar image TCD · Jul 22, 2014 at 09:23 AM 0
Share
  1. Pan is a Crater

  2. change the Terrain size in the editor befor running the game/script

  3. 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 do xRes = 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);

avatar image CScherer549 · Jul 22, 2014 at 06:15 PM 0
Share

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.

avatar image TCD · Jul 22, 2014 at 08:06 PM 0
Share

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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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