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 STApps · Aug 22, 2014 at 04:52 PM · javascriptterrainramp

Generating ramps on a terrain using scripts

I've spend the whole day looking for a solution and the only thing that I got was some king of editor addon to draw ramps, so I'm asking for your help.

I've got a plain empty terrain and I'd like to draw 2 ramps on it, based on some parameters. Everything must be done through scripts, since the ramps must be generated by user's input.

The idea is to have a method like this one

 private function GenerateRamps(upLength : float, upAngle : float, upHeight : float, distance: float, downLength: float, downAngle : float, downHeight: float)

that generates the two ramps, one that goes up while the other goes down, and puts them at a fixed distance (the distance parameter).

Angles are expressed in degrees while heights, lenghts and distance in meters.

Can you give some hint on this? I really have no clue on where to start from.

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

Answer by robertbu · Aug 22, 2014 at 05:03 PM

With a terrain you are not going to get precisely what you want. The heights in a terrain are controlled by a heightmap. A heightmap is a regularly spaced grid of heights.

You specify the height at each pin, so any distances (ramp width, ramp distance, distances between ramps) will need to be rounded to fit on the heightmap grid. Take a look at the answer by @alucardj here:

http://answers.unity3d.com/questions/412624/how-can-i-make-my-algorithm-make-cloudy-heightmaps.html

He's setting the heightmap using perlin noise, but this answer gives you the framework...how to get the height map, how to set values, how to apply those values.

For the heights of the ramp itself, you can just use Lerp() or calculate the linear progression by hand. While you did not ask for this, once you get the what you are doing working, it would be easy to apply a mathematical curve to the height values to produce ramps of different shapes.

[1]: http://g-ecx.images-amazon.com/images/G/01/ciu/46/84/c47f820dd7a0241f7b6de010.L.jpg

Comment
Add comment · Show 4 · 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 STApps · Aug 23, 2014 at 08:46 AM 0
Share

Thank you for your reply.

I've been playing with that link for some time but it seems that the script modifies the terrain leaving some empty space under.

$$anonymous$$y idea is to build something like the result of this tool

http://forum.unity3d.com/threads/released-rampbrush-a-sculpting-tool-for-creating-slopes-and-ramps-on-terrains.114586/

but by code and not with the editor. Think of them as the ramps that you see in some motocross tracks.

Changing the heightmap gives a different result than the one that you get with the Raise/Lower Terrain tool, which is more realistic.

Please keep in $$anonymous$$d that I'm just moving my first steps in Unity3D and I've got a lot of things to learn but, unfortunately, I really need to do this thing

avatar image robertbu · Aug 23, 2014 at 01:51 PM 0
Share

For $10, you can get their tool and take a look at what they are doing in code. Based only on the images, what I see is rounded corners and perhaps sloped sided. You could do this easily by mapping your heights through a curve (a mentioned that in my answer). Looking at the pictures, using a AnamationCurve to define both horizontal and vertical shape should get you something similar to what is in the image.

avatar image robertbu · Aug 24, 2014 at 09:33 PM 0
Share

Here is an example script using an AnimationCurve to form the ramps. Attach it to a Terrain and add a directional light.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (Terrain))]
 public class Ramp : $$anonymous$$onoBehaviour {
 
     public Terrain terrain;
     public AnimationCurve acProfile;
     public AnimationCurve acFront;
     private TerrainData terrainData;
 
     void Start () {
 
         terrain = GetComponent<Terrain>();
 
         terrainData = terrain.terrainData;
         
         BuildRamp(231,  25, 50, 175, 0.3f, false);
         BuildRamp(231, 315, 50, 175, 0.3f, true);
     }
 
     void BuildRamp(int x, int y, int dx, int dy, float height, bool reverse) {
 
         float[,] data = terrainData.GetHeights( x, y, dx, dy );
 
         for (int i = 0; i < dx; i++) {
             for (int j = 0; j < dy; j++) {
                 float f1 = (float)i / ((float)(dx - 1));
                 float f2 = (float)j / ((float)(dy - 1));
                 if (reverse) f2 = 1.0f - f2;
                 data[j,i] = acProfile.Evaluate(f2) * acFront.Evaluate (f1) * height;
             }
         }
         terrainData.SetHeights (x,y,data);
     }
 }

Here are the curves I used...profile on the left and front on the right:

alt text

And here is the result. Note the rounded corners:

alt text

ac_curves.png (38.8 kB)
ramps.png (20.9 kB)
avatar image STApps · Mar 12, 2015 at 12:04 PM 0
Share

I don't know why I didn't get a notification for your comment because it's awesome. I have 2 issues though:

  • my vehicle passes through the ramps because it seems that the collider doesn't get updated.

  • can I set ramp's height in meters?

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Terrain texture specific step sounds? 1 Answer

DDT script only works on one terrain 1 Answer

Collision with Terrain 2 Answers

Raycast to Terrain (Conditional Statements) 1 Answer


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