|
How can you detect if a character walk on for example grass, gravel or sand? Currently it seems that you can only assign one physics material or tag for one big terrain?
(comments are locked)
|
|
This is slightly trickier than it might initially seem - partly because any given location on the terrain can be a mixture of two or more types of terrain, and partly because the data used to store this information is relatively complex. Briefly, you need to read Alpha Map data from the terrain class using the GetAlphaMaps function. The alphamap data is a grid of values spread over the area of the terrain, and each entry in the grid contains the values used to "mix" each of the different terrain textures, so more specifically you need to calculate which grid cell your player is in, then read the mix values for that grid cell. I've written about this in more detail here, however I'm tempted to wrap this up quickly into a helper function which can be used without getting knee deep in the terrain API :) -- EDIT -- Here you go. Add the large script below in as a new C# script in your project. Name the file "TerrainSurface". You'll then be able to use these helper functions to read either the mix values of textures, or just get the most dominant texture index at a given world position. Eg:
Your "surfaceIndex" will now contain an integer denoting the zero-based-index of the texture which is the most dominant texture at that location. If you have 4 textures added to your terrain, the value will be one of 0,1,2 or 3. Alternatively you can read the actual mix of textures, like this:
Your "surfaceMix" will contain an array of floating point values, denoting the relative mix of each texture at that location. For example, if you have 4 textures, and the 4th texture is "Mud", you could use this to determine how muddy the current location is, like this: (remember, the indices are zero-based, so the 4th texture has an index of 3)
And below is the main helper script. Add this in as a new C# script in your project. Name the file "TerrainSurface". You can use the functions from C# scripts straight off. If you want to use these function from other Javascript scripts (as in the examples above), you need to put the script in a folder that gives it an earlier compilation pass. To do this, make a folder in your Project called "Plugins" and put the C# TerrainSurface script in there. Enjoy! This is how I would do it too. Tough one thing you ll have to note is GetAlphaMaps and all its class of functions aren't so-called official scripts so if changes come about in further versions of unity they aren't guaranteed to work. Sometimes, I have left over materials in my terrain on editor mode that I created at run-time dynamically so be sure to check after use.
Nov 17 '10 at 10:17 AM
denewbie
1 more thing. To easily see what functions you have available in the API could I suggest that you view your scripts via Visual Studios. That way, these scripts can also be displayed when you press "." so you wont have a hard time figuring out whats available and stuff...
Nov 17 '10 at 10:21 AM
denewbie
Oh 1 more thing to add. IF you re going for large scale and real terrain data you might want to look at this concept we call shapefiles: http://en.wikipedia.org/wiki/Shapefile
Nov 17 '10 at 10:33 AM
denewbie
Updated with helper script & examples!
Nov 17 '10 at 11:22 AM
duck ♦♦
(comments are locked)
|
|
Here is my JavaScript translation of the same cs code: Thanks Ben! (works splendidly) ! // -- TerrainSurface.js -- // // Ben Pitt // // JS translation by Steven Mikkelson @MartianGames :) // public class TerrainSurface { }
(comments are locked)
|
|
I would suggest that you place a collider or something like that over, for example grass. Then give that collider a tag of Grass, or a name of Grass. Then in your script you have something like:
And place that within your Player EDIT--- With that in place you can do something like this: var PlayGrass = false;
Rough Coding there, but you get the picture
(comments are locked)
|
I can not fully understand it, could you explain it for me? Thanks a lot.
(comments are locked)
|
