full C# Code for infinite voxel terrain generation
- User
full C# Code for infinite voxel terrain generation
- Assistant
Here's an example of C# code for infinite voxel terrain generation using Perlin noise:
using UnityEngine; public class VoxelTerrain : MonoBehaviour { public int chunkSize = 16; public int viewDistance = 4; public float noiseScale = 0.1f; private Vector3Int currentPlayerChunk; private Dictionary<Vector3Int, Chunk> chunks = new Dictionary<Vector3Int, Chunk>(); private void Start() { currentPlayerChunk = GetChunkPositionFromWorldPosition(transform.position); GenerateChunksAroundPlayer(); } private void Update() { Vector3Int newPlayerChunk = GetChunkPositionFromWorldPosition(transform.position); if (newPlayerChunk != currentPlayerChunk) { currentPlayerChunk = newPlayerChunk; GenerateChunksAroundPlayer(); } } private void GenerateChunksAroundPlayer() { int halfDistance = Mathf.FloorToInt(viewDistance / 2); for (int x = -halfDistance; x <= halfDistance; x++) { for (int z = -halfDistance; z <= halfDistance; z++) { Vector3Int chunkPos = currentPlayerChunk + new Vector3Int(x, 0, z); if (!chunks.ContainsKey(chunkPos)) { GenerateChunk(chunkPos); } } } } private void GenerateChunk(Vector3Int chunkPos) { Chunk chunk = new Chunk(chunkSize, noiseScale, chunkPos); chunks.Add(chunkPos, chunk); chunk.Generate(); } private Vector3Int GetChunkPositionFromWorldPosition(Vector3 position) { int x = Mathf.FloorToInt(position.x / chunkSize); int y = Mathf.FloorToInt(position.y / chunkSize); int z = Mathf.FloorToInt(position.z / chunkSize); return new Vector3Int(x, y, z); } } public class Chunk { private int size; private float scale; private Vector3Int position; private ChunkData data; public Chunk(int size, float scale, Vector3Int position) { this.size = size; this.scale = scale; this.position = position; this.data = new ChunkData(size); } public void Generate() { for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { for (int z = 0; z < size; z++) { float noiseValue = Mathf.PerlinNoise((position.x + x) * scale, (position.z + z) * scale); int height = Mathf.FloorToInt(noiseValue * size); if (y <= height) { data.SetBlock(x, y, z, true); } } } } // TODO: Generate mesh for the chunk using voxel data } } public class ChunkData { private bool[,,] voxels; public ChunkData(int size) { voxels = new bool[size, size, size]; } public void SetBlock(int x, int y, int z, bool isActive) { voxels[x, y, z] = isActive; } public bool GetBlock(int x, int y, int z) { return voxels[x, y, z]; } }This code uses a
VoxelTerrainclass to manage the generation and rendering of all the chunks. Each chunk is represented by theChunkclass, which contains the voxel data and generates the terrain using Perlin noise. TheChunkDataclass stores the state of each voxel in the chunk.The
VoxelTerrainclass generates the chunks around the player's position and keeps track of them in a dictionary. It also updates the generated chunks when the player moves. You can modify theGenerateChunkmethod to create the mesh for each chunk based on the voxel data.Please note that this code provides a basic implementation of infinite voxel terrain generation using Perlin noise. Depending on your requirements, you may need to add additional features such as chunk meshing and LOD (Level of Detail) management for performance optimization.
- User
C# code for throttle opening animation when up key pressed to control locomotive
- Assistant
To achieve a throttle opening animation when the up key is pressed to control a locomotive, you can use Unity's animation system. Here's an example C# code that demonstrates how to do this:
using UnityEngine; public class LocomotiveController : MonoBehaviour { public float speed = 5f; public AnimationCurve throttleCurve; private Animator animator; private float currentThrottle = 0f; private void Start() { animator = GetComponent<Animator>(); } private void Update() { float input = Input.GetAxis("Vertical"); if (input > 0) { currentThrottle += input * speed * Time.deltaTime; currentThrottle = Mathf.Clamp01(currentThrottle); float throttleValue = throttleCurve.Evaluate(currentThrottle); animator.SetFloat("Throttle", throttleValue); } else { currentThrottle = 0f; animator.SetFloat("Throttle", 0f); } } }In this code, a locomotive game object is assumed to have an Animator component attached to it. Additionally, an AnimationCurve is used to control the throttle opening animation. You can customize the
speedandthrottleCurvevariables in the Inspector according to your preferences.The
Startmethod gets a reference to the Animator component for the locomotive. TheUpdatemethod checks for the up arrow key being pressed (a positive input value). When the up key is pressed, the current throttle value is incremented based on the input, clamped to a range of 0 to 1, and the throttle curve is evaluated to determine the throttle animation value. This throttle value is then set to the "Throttle" parameter of the Animator.When the up key is released, the current throttle value is reset to 0, and the "Throttle" parameter is set to 0 to close the throttle animation.
Make sure to set up your locomotive model and Animator controller to properly handle the "Throttle" parameter and the corresponding animation states.
- User
C# code for releasing steam from cylinders locomotive when controlled forward and reverse.
- Assistant
Here's an example of C# code that demonstrates how to release steam from cylinders of a locomotive when it is controlled in forward and reverse:
using UnityEngine; public class LocomotiveSteam : MonoBehaviour { public ParticleSystem steamEmitter; public KeyCode forwardKey = KeyCode.W; public KeyCode reverseKey = KeyCode.S; public float steamEmissionRate = 10f; private void Update() { if (Input.GetKey(forwardKey)) { steamEmitter.emissionRate = steamEmissionRate; } else if (Input.GetKey(reverseKey)) { steamEmitter.emissionRate = steamEmissionRate; } else { steamEmitter.emissionRate = 0f; } } }In this code, a locomotive game object is assumed to have a ParticleSystem component attached to it, representing the steam emission from the cylinders. The
steemEmittervariable should be assigned with this ParticleSystem component in the Inspector.The
Updatemethod checks for the forward and reverse keys being pressed (forwardKeyandreverseKeyrespectively). When either of these keys is pressed, the steam emission rate of the ParticleSystem is set tosteamEmissionRate, indicating that steam is being released from the cylinders. When neither of the keys is pressed, the steam emission rate is set to 0, stopping the steam release.Make sure to set up the ParticleSystem component with the desired settings and emission rate properties to achieve the desired visual effect of steam release from the cylinders.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.