TerrainDeformer.cs using UnityEngine; public class TerrainDeformer : MonoBehaviour {…
- User
TerrainDeformer.cs
using UnityEngine;
public class TerrainDeformer : MonoBehaviour { public Terrain terrain; public float deformationRadius = 0.5f; public float deformationIntensity = 0.2f;
codeprivate TerrainData terrainData; private int heightmapWidth; private int heightmapHeight; private float[,] originalHeights; void Start() { terrainData = terrain.terrainData; heightmapWidth = terrainData.heightmapResolution; heightmapHeight = terrainData.heightmapResolution; originalHeights = terrainData.GetHeights(0, 0, heightmapWidth, heightmapHeight); } public void DeformTerrain(Vector3 position, float radius, float intensity) { Vector3 terrainPos = position - terrain.transform.position; int posX = Mathf.RoundToInt(terrainPos.x / terrainData.size.x * heightmapWidth); int posZ = Mathf.RoundToInt(terrainPos.z / terrainData.size.z * heightmapHeight); int deformRadius = Mathf.RoundToInt(radius / terrainData.size.x * heightmapWidth); int startX = Mathf.Max(0, posX - deformRadius); int endX = Mathf.Min(heightmapWidth, posX + deformRadius); int startZ = Mathf.Max(0, posZ - deformRadius); int endZ = Mathf.Min(heightmapHeight, posZ + deformRadius); float[,] heights = terrainData.GetHeights(startX, startZ, endX - startX, endZ - startZ); float intensityMultiplier = intensity / deformRadius; for (int x = 0; x < endX - startX; x++) { for (int z = 0; z < endZ - startZ; z++) { float distance = Vector2.Distance(new Vector2(x, z), new Vector2(posX - startX, posZ - startZ)); if (distance < deformRadius) { float deformAmount = intensity * (1.0f - (distance / deformRadius)); heights[z, x] -= deformAmount * intensityMultiplier; } } } terrainData.SetHeights(startX, startZ, heights); } // Call this method when you want to reset the terrain public void ResetTerrain() { terrainData.SetHeights(0, 0, originalHeights); } void OnApplicationQuit() { ResetTerrain(); } Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.