| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | public class SpacialMovement : MonoBehaviour |
| | | 4 | | { |
| | 3 | 5 | | public float moveSpeed = 10.0f; // Adjust this to control movement speed. |
| | 3 | 6 | | public float rotationSpeed = 2.0f; // Adjust this to control rotation speed. |
| | 3 | 7 | | public Vector2 limitX = new Vector2(490f, 560f); |
| | 3 | 8 | | public Vector2 limitY = new Vector2(3.5f, 15f); |
| | 3 | 9 | | public Vector2 limitZ = new Vector2(515f, 570f); |
| | 3 | 10 | | public float delayToStartMovement = 3f; |
| | 3 | 11 | | private float rotationX = 0.0f; |
| | 3 | 12 | | private float rotationY = 0.0f; |
| | 3 | 13 | | private bool allowMovement = false; |
| | | 14 | | |
| | | 15 | | private void Start() |
| | 2 | 16 | | { |
| | 2 | 17 | | Cursor.visible = false; |
| | 2 | 18 | | Invoke("startMovement", delayToStartMovement); |
| | 2 | 19 | | rotationX = transform.rotation.eulerAngles.x; |
| | 2 | 20 | | rotationY = transform.rotation.eulerAngles.y; |
| | 2 | 21 | | } |
| | | 22 | | |
| | | 23 | | private void Update() |
| | 205 | 24 | | { |
| | 330 | 25 | | if (!allowMovement) return; |
| | | 26 | | // Handle movement using WASD or arrow keys. |
| | 80 | 27 | | float horizontalInput = Input.GetAxis("Horizontal"); |
| | 80 | 28 | | float verticalInput = Input.GetAxis("Vertical"); |
| | | 29 | | |
| | 80 | 30 | | Vector3 movement = new Vector3(horizontalInput, 0.0f, verticalInput) * moveSpeed * Time.deltaTime; |
| | 80 | 31 | | transform.Translate(movement); |
| | | 32 | | |
| | | 33 | | // Get mouse input |
| | 80 | 34 | | float mouseX = Input.GetAxis("Mouse X"); |
| | 80 | 35 | | float mouseY = Input.GetAxis("Mouse Y"); |
| | | 36 | | |
| | | 37 | | // Calculate the new rotations |
| | 80 | 38 | | rotationY += mouseX * rotationSpeed; |
| | 80 | 39 | | rotationX -= mouseY * rotationSpeed; |
| | | 40 | | |
| | | 41 | | // Clamp the vertical rotation to prevent flipping |
| | 80 | 42 | | rotationX = Mathf.Clamp(rotationX, -90f, 90f); |
| | | 43 | | |
| | | 44 | | // Apply the rotations to the GameObject |
| | 80 | 45 | | transform.rotation = Quaternion.Euler(rotationX, rotationY, 0.0f); |
| | | 46 | | |
| | | 47 | | // Handle upward movement using the space key. |
| | 80 | 48 | | if (Input.GetKey(KeyCode.Space)) |
| | 0 | 49 | | { |
| | 0 | 50 | | Vector3 upMovement = Vector3.up * moveSpeed * Time.deltaTime; |
| | 0 | 51 | | transform.Translate(upMovement); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | // Handle downward movement using the left control key. |
| | 80 | 55 | | if (Input.GetKey(KeyCode.LeftControl)) |
| | 0 | 56 | | { |
| | 0 | 57 | | Vector3 downMovement = Vector3.down * moveSpeed * Time.deltaTime; |
| | 0 | 58 | | transform.Translate(downMovement); |
| | 0 | 59 | | } |
| | | 60 | | |
| | 80 | 61 | | transform.position = new Vector3 |
| | | 62 | | ( |
| | | 63 | | Mathf.Clamp(transform.position.x, limitX.x, limitX.y), |
| | | 64 | | Mathf.Clamp(transform.position.y, limitY.x, limitY.y), |
| | | 65 | | Mathf.Clamp(transform.position.z, limitZ.x, limitZ.y) |
| | | 66 | | ); |
| | | 67 | | |
| | 80 | 68 | | if(Input.GetKeyDown("escape")) |
| | 0 | 69 | | { |
| | 0 | 70 | | Application.Quit(); |
| | 0 | 71 | | } |
| | 205 | 72 | | } |
| | | 73 | | |
| | | 74 | | private void startMovement() |
| | 1 | 75 | | { |
| | 1 | 76 | | allowMovement = true; |
| | 1 | 77 | | } |
| | | 78 | | } |
| | | 79 | | |