Skip to content

Instantly share code, notes, and snippets.

@Cr0a3
Created January 19, 2025 18:33
Show Gist options
  • Select an option

  • Save Cr0a3/6ac4bc5e92cd8326a74da7bf34c4d8c3 to your computer and use it in GitHub Desktop.

Select an option

Save Cr0a3/6ac4bc5e92cd8326a74da7bf34c4d8c3 to your computer and use it in GitHub Desktop.
using PlanetaryTerrain;
using Unity.VisualScripting;
using UnityEngine;
public class VehicleCameraController : MonoBehaviour
{
public float zoomSpeed;
public float orbitSize;
public Transform orbitMiddle;
[SerializeField] Planet planet;
public float sensitivity = 2f;
public GameObject build;
void Start()
{
this.build = GameObject.Find("Build");
}
// Update is called once per frame
void Update()
{
this.Zoom();
if (Input.GetMouseButton((int)MouseButton.Left))
this.Rotate();
if (Input.GetMouseButtonUp((int)MouseButton.Left))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
private void FixedUpdate()
{
this.orbitMiddle.position = this.build.transform.position;
}
public void Rotate()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
transform.RotateAround(orbitMiddle.position, transform.up, mouseX * this.sensitivity * 100f * Time.deltaTime);
transform.RotateAround(orbitMiddle.position, transform.right, -mouseY * this.sensitivity * 100f * Time.deltaTime);
}
private void Zoom()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
this.orbitSize -= scroll * this.zoomSpeed;
this.orbitSize = Mathf.Clamp(this.orbitSize, 2f, 50f);
Vector3 direction = (transform.position - this.orbitMiddle.position).normalized;
transform.position = this.orbitMiddle.position + direction * this.orbitSize;
transform.LookAt(this.orbitMiddle);
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(this.orbitMiddle.position, this.orbitSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment