- Home /
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.
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
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
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
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.
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:
And here is the result. Note the rounded corners:
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
Follow this Question
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