3d-test/Assets/CameraController_20240419.cs

86 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController_20240419 : MonoBehaviour
{
// private readonly float speed_move = 3.0f;
// private readonly float speed_rota = 2.0f;
// void MoveObjectFunc()
// {
// float keyH = Input.GetAxis("Horizontal");
// float keyV = Input.GetAxis("Vertical");
// keyH = keyH * speed_move * Time.deltaTime;
// keyV = keyV * speed_move * Time.deltaTime;
// transform.Translate(Vector3.right * keyH);
// transform.Translate(Vector3.forward * keyV);
// float mouseX = Input.GetAxis("Mouse X");
// float mouseY = Input.GetAxis("Mouse Y");
// transform.Rotate(Vector3.up * speed_rota * mouseX);
// transform.Rotate(Vector3.left * speed_rota * mouseY);
// }
// // Use this for initialization
// void Start()
// {
// }
// // Update is called once per frame
// void Update()
// {
// MoveObjectFunc();
// }
public float rotateSpeed = 2.0f;
public float limitAngle = 70.0f;
private bool isRotate;
private float mouseX;
private float mouseY;
public float scrollSpeed = 20000.0f;
private void Start()
{
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
isRotate = true;
}
if (Input.GetMouseButtonUp(0))
{
isRotate = false;
}
if (isRotate)
{
Rotation();
}
else
{
Scroll();
}
}
public void Rotation()
{
mouseX += Input.GetAxis("Mouse X") * rotateSpeed; // AxisX = Mouse Y
mouseY = Mathf.Clamp(mouseY + Input.GetAxis("Mouse Y") * rotateSpeed, -limitAngle, limitAngle);
transform.rotation = Quaternion.Euler(transform.rotation.x - mouseY, transform.rotation.y + mouseX, 0.0f);
}
public void Scroll()
{
float scroollWheel = Input.GetAxis("Mouse ScrollWheel");
Vector3 cameraDirection = this.transform.localRotation * Vector3.forward;
this.transform.position += cameraDirection * Time.deltaTime * scroollWheel * scrollSpeed;
}
}