- Home /
Dynamic Terrain Painting
Hi, I need to be able to generate dynamic frozen ground on the terrain that slows everything passing through, for that I have to be able to temporarily set terrain's texture via script in certain radius from the target. Is this possible to implement on the standard existing Terrain game object?
Answer by thormond · May 23, 2014 at 11:03 PM
correct answer was Projector (with some UnityPro or custom shader) combined with sphere collider https://docs.unity3d.com/Documentation/Components/class-Projector.html
Answer by Hyperion · Sep 01, 2013 at 04:19 PM
It's possible if you assign your terrain multiple textures. Say texture 0 is ground and texture 1 is frozen-ground. In script, you can access which one you're using.
Well I'm already using multiple terrain textures via editor UI, what i want to know is: how do I take texture 1 and tell unity with script to draw with this texture on the terrain in certain radius ? ( and then to undo this change after X seconds )
You should use the 'add new comment' button next time. Anyway, I'll just show you a script that changes texture upon mouse click using the second texture.
//attach this to camera
static var mousepos : Vector3;
function Update ()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit,1000))
{
mousepos = hit.point;
}
}
Then:
//attach this to terrain
var saved : float[,,];
private var tData : TerrainData;
var cratertex : Texture2D;
var craterData;
var xRes: int;
var yRes: int;
var layers: int;
function Start () {
tData = Terrain.activeTerrain.terrainData;
yRes = tData.alphamapHeight;
xRes = tData.alphamapWidth;
layers = tData.alphamapLayers;
craterData = cratertex.GetPixels();
saved = tData.GetAlphamaps (0, 0, xRes, yRes);
}
function OnApplicationQuit () {
tData.SetAlphamaps (0,0,saved);
}
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)){
var g : int = $$anonymous$$athf.Lerp(0, xRes, $$anonymous$$athf.InverseLerp(0, tData.size.x, mouse.mousepos.x));
var b : int = $$anonymous$$athf.Lerp(0, yRes, $$anonymous$$athf.InverseLerp(0, tData.size.z, mouse.mousepos.z));
g = $$anonymous$$athf.Clamp(g, cratertex.width/2, xRes-cratertex.width/2);
b = $$anonymous$$athf.Clamp(b, cratertex.height/2, yRes-cratertex.height/2);
var area = tData.GetAlphamaps(g-cratertex.width/2, b-cratertex.height/2, cratertex.width, cratertex.height);
for (x = 0; x < cratertex.height; x++) {
for (y = 0; y < cratertex.width; y++) {
for (z = 0; z < layers; z++){
if (z == 1){
area [x,y,z] += craterData[x*cratertex.width + y].a;
}
else{
area [x,y,z] -= craterData[x*cratertex.width + y].a;
}
}}}
tData.SetAlphamaps (g-cratertex.width/2,b-cratertex.height/2,area);
}
}
And the script will require a 'texture' so I'll attach it.
Important: $$anonymous$$ake sure the texture import settings are:
Add the texture to the script.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to import the object from server to unity 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Material doesn't have a color property '_Color' 4 Answers
Setting Scroll View Width GUILayout 1 Answer