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 EuroFightX · Aug 18, 2014 at 01:07 PM · terrainruntime

Smooth Terrain Increase

Hi guys,

I have written a script for modifying the terrain level at runtime. Unfortunately, however, the terrain increases as a square instead of the desirable smooth circle, like in the built in terrain modification script.

 using UnityEngine;
 using System.Collections;
 
 public class TerrainEdit : MonoBehaviour {
 
     public Terrain terr;
     public int size = 20;
     // Use this for initialization
     void Start() {
 
     }
     
     // Update is called once per frame
     void Update() {
         if (Input.GetButton("Fire1") | Input.GetButton("Fire2")) {
             editTerrain ();
         }
     }
 
     void editTerrain() {
         int posX = (int)(((transform.position.x/terr.terrainData.size.x)*terr.terrainData.heightmapWidth)-(size/2f));
         int posZ = (int)(((transform.position.z/terr.terrainData.size.z)*terr.terrainData.heightmapWidth)-(size/2f));
         int xResolution = size;
         int zResolution = size;
         float[,] heights = terr.terrainData.GetHeights (posX, posZ, xResolution, zResolution);
 
         float increase;
 
         if (Input.GetButton("Fire2")) {
             increase = -0.001f;
         } else {
             increase = 0.001f;
         }
 
         int centX = posX - (size / 2);
         int centZ = posZ - (size / 2);
 
         for (int z = 0; z < zResolution; z++) {
             for (int x=0; x < xResolution; x++) {
                 float idealHeight = heights[x, z] + increase;
                 float dist = 1;
                 float mult = 1;
                 if (dist > 1) {
                     mult = 1/dist;
                 } else if (dist > size/4) {
                     mult = 0;
                 }
                 heights[x, z] = idealHeight * mult;
             }
         }
 
         terr.terrainData.SetHeights(posX, posZ, heights);
     }
 }
 

Any help would be greatly appreaciated

Comment
Add comment · Show 4
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 OrbitGames · Aug 18, 2014 at 02:56 PM 0
Share

I don't understand what you are trying to do between line 40 and 47... dist is always going to be 1...

avatar image EuroFightX · Aug 18, 2014 at 04:32 PM 0
Share

That section is when I attempted to implement the smooth increase by myself by calculating the distance from the centre of the terrain to raise and then decreasing the multiplier the further away the function gets from the center.

For example, if the point is 3 units away from the center it will be increased by 1/3 of the amount of the point at the center.

Am I doing this wrong?

avatar image OrbitGames · Aug 19, 2014 at 12:25 PM 0
Share

Do you want to recreate the builtin terrainbrush ingame or do you just want the terrain to be a big hill?

avatar image EuroFightX · Aug 31, 2014 at 12:22 AM 0
Share

I would like to recreate the terrain brush effect (ideally)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OrbitGames · Aug 31, 2014 at 02:40 PM

(outside the for loops)check where the mouse is clicking(use raycasting) on the terrain

find world coordinates of terrain coordinate(don't know how to call it)(tutorial to do the reversed thing here)

get the distance between the x & z coordinates of the upper two

then (if the distance is between 0 and max distance) heights[i,j] +=(1-(maxdistance / distance) * heaviness) to the height;

This will result in a cone like rise...

I'm not sure it will work but you can try this formula instead:

heights[i,j] += 2*(distance/maxdistance)^3-3*(distance/maxdistance)^2+1;

hope this helps

(everything is untested)

Comment
Add comment · 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

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

Paint Terrain Texture From Vector3 List 0 Answers

lowering Terrain at run-time, C# 0 Answers

How to make trees in a terrain transparent in runtime? 0 Answers

How to add new baked lightmaps to lightmap array? 2 Answers

Modify a mesh at runtime 0 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