VR_Terrain/Assets/CameraScript.cs

117 lines
4.2 KiB
C#
Raw Normal View History

2023-12-03 16:20:13 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class CameraScript : MonoBehaviour
{
PlayerControls controls;
private Vector3 cameraStartingPosition;
private Quaternion cameraStartingRotation;
public float moveSpeed = 200f; // Adjust this to control the speed of movement
public float rotationSpeed = 100f; // Adjust this to control the speed of camera rotation
Vector2 moveInput = Vector2.zero;
Vector2 rotateInput = Vector2.zero;
float heightChange = 0f;
float rollChange = 0f;
void Awake()
{
controls = new PlayerControls();
cameraStartingPosition = transform.position;
cameraStartingRotation = transform.rotation;
2023-12-03 21:28:35 +01:00
// Gamepad (normal)
2023-12-03 16:20:13 +01:00
controls.Gameplay.Move.performed += ctx => moveInput = ctx.ReadValue<Vector2>();
controls.Gameplay.Move.canceled += ctx => moveInput = Vector2.zero;
controls.Gameplay.Rotate.performed += ctx => rotateInput = ctx.ReadValue<Vector2>();
controls.Gameplay.Rotate.canceled += ctx => rotateInput = Vector2.zero;
controls.Gameplay.Up.performed += ctx => heightChange = 1f;
controls.Gameplay.Up.canceled += ctx => heightChange = 0f;
controls.Gameplay.Down.performed += ctx => heightChange = -1f;
controls.Gameplay.Down.canceled += ctx => heightChange = 0f;
controls.Gameplay.CameraRotationLeft.performed += ctx => rollChange = 1f;
controls.Gameplay.CameraRotationLeft.canceled += ctx => rollChange = 0f;
controls.Gameplay.CameraRotationRight.performed += ctx => rollChange = -1f;
controls.Gameplay.CameraRotationRight.canceled += ctx => rollChange = 0f;
controls.Gameplay.resetCameraPosition.performed += ctx => ResetCameraPosition();
controls.Gameplay.resetCameraRotation.performed += ctx => ResetCameraRotation();
2023-12-03 21:28:35 +01:00
//keyboard (afterthought)
controls.Keyboard.Move.performed += ctx => moveInput = ctx.ReadValue<Vector2>();
controls.Keyboard.Move.canceled += ctx => moveInput = Vector2.zero;
controls.Keyboard.Rotate.performed += ctx => rotateInput = ctx.ReadValue<Vector2>();
controls.Keyboard.Rotate.canceled += ctx => rotateInput = Vector2.zero;
controls.Keyboard.Up.performed += ctx => heightChange = 1f;
controls.Keyboard.Up.canceled += ctx => heightChange = 0f;
controls.Keyboard.Down.performed += ctx => heightChange = -1f;
controls.Keyboard.Down.canceled += ctx => heightChange = 0f;
controls.Keyboard.CameraRotationLeft.performed += ctx => rollChange = 1f;
controls.Keyboard.CameraRotationLeft.canceled += ctx => rollChange = 0f;
controls.Keyboard.CameraRotationRight.performed += ctx => rollChange = -1f;
controls.Keyboard.CameraRotationRight.canceled += ctx => rollChange = 0f;
controls.Keyboard.resetCameraPosition.performed += ctx => ResetCameraPosition();
controls.Keyboard.resetCameraRotation.performed += ctx => ResetCameraRotation();
2023-12-03 16:20:13 +01:00
}
2023-12-08 08:14:16 +01:00
void FixedUpdate()
2023-12-03 16:20:13 +01:00
{
// Move the object along the X and Z axes based on left stick input
Vector3 moveDirection = new Vector3(moveInput.x, heightChange, moveInput.y);
moveDirection = transform.TransformDirection(moveDirection);
transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
// Rotate the object based on right stick input
Vector3 rotateDirection = new Vector3(-rotateInput.y, rotateInput.x, rollChange);
// Apply the rotate direction with speed and time in local space
transform.Rotate(rotateDirection * rotationSpeed * Time.deltaTime, Space.Self);
}
void ResetCameraPosition()
{
transform.position = cameraStartingPosition;
transform.rotation = cameraStartingRotation;
}
void ResetCameraRotation()
{
// Preserve the current Y rotation
Quaternion newRotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0f);
transform.rotation = newRotation;
}
void OnEnable()
{
controls.Gameplay.Enable();
2023-12-03 21:28:35 +01:00
controls.Keyboard.Enable();
2023-12-03 16:20:13 +01:00
}
void OnDisable()
{
controls.Gameplay.Disable();
2023-12-03 21:28:35 +01:00
controls.Keyboard.Disable();
2023-12-03 16:20:13 +01:00
}
}