Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- public class KeyboardPlacement : MonoBehaviour {
- public GameObject centerObject; // The game object around which the keys will be placed
- public float radius = 2.0f; // The radius of the hemisphere
- public float height = 1.0f; // The height of the hemisphere
- public float keySpacing = 0.1f; // The spacing between each key
- public float horizontalDegrees = 80.0f; // The angle in degrees of the left and right edges of the keyboard
- public float verticalDegrees = 60.0f; // The angle in degrees of the top and bottom edges of the keyboard
- private List<Vector3> positions = new List<Vector3>();
- private string[] keys = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "z", "x", "c", "v", "b", "n", "m", ",", ".", "-" };
- private bool keysCreated = false;
- //void OnDrawGizmosSelected() {
- // if (GUILayout.Button("Create Keyboard")) {
- // CreateKeys();
- // }
- //}
- private void Start() {
- CreateKeys();
- }
- void CalculatePositions() {
- // Calculate the number of rows and columns of keys
- int numRows = 4;
- int[] numColumns = { 10, 10, 10, 10 };
- // Calculate the angle between each key
- float[] horizontalAngle = new float[] {
- horizontalDegrees / (numColumns[0] - 1),
- horizontalDegrees / (numColumns[1] - 1),
- horizontalDegrees / (numColumns[2] - 1),
- horizontalDegrees / (numColumns[3] - 1)
- };
- float verticalAngle = verticalDegrees / (numRows - 1);
- // Calculate the position of each key
- int keyIndex = 0;
- for (int row = numRows - 1; row >= 0; row--) {
- float rowOffset = 0f;
- if (row == 1) {
- rowOffset = horizontalAngle[0] / 2;
- } else if (row == 2) {
- rowOffset = horizontalAngle[1] / 2;
- } else if (row == 3) {
- rowOffset = horizontalAngle[2] / 2;
- }
- // Align the middle column to be between G and H
- float centerColumnOffset = 0.5f * horizontalAngle[1];
- for (int col = 0; col < numColumns[row]; col++) {
- float x = radius * Mathf.Cos(Mathf.Deg2Rad * verticalAngle * (row - (numRows - 1) / 2)) * Mathf.Sin(Mathf.Deg2Rad * (horizontalAngle[row] * col + rowOffset - (numColumns[row] - 1) / 2 * horizontalAngle[row] - centerColumnOffset));
- float y = radius * Mathf.Sin(Mathf.Deg2Rad * verticalAngle * (row - (numRows - 1) / 2));
- float z = radius * Mathf.Cos(Mathf.Deg2Rad * verticalAngle * (row - (numRows - 1) / 2)) * Mathf.Cos(Mathf.Deg2Rad * (horizontalAngle[row] * col + rowOffset - (numColumns[row] - 1) / 2 * horizontalAngle[row] - centerColumnOffset));
- Vector3 position = new Vector3(x, y, z);
- // Rotate the position around the center object's forward vector
- position = Quaternion.LookRotation(centerObject.transform.forward) * position;
- position += centerObject.transform.position;
- positions.Add(position);
- keyIndex++;
- }
- }
- }
- void InstantiateKeys() {
- GameObject cubePrefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
- cubePrefab.transform.localScale = Vector3.one * 0.2f;
- for (int i = 0; i < keys.Length; i++) {
- GameObject key = new GameObject(keys[i]);
- key.transform.position = positions[i];
- key.transform.parent = transform;
- GameObject cube = Instantiate(cubePrefab, key.transform.position, Quaternion.identity);
- cube.transform.parent = key.transform;
- }
- }
- void CreateKeys() {
- CalculatePositions();
- InstantiateKeys();
- keysCreated = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement