34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
public float RotationSpeed;
|
|
public float PositionSpeed;
|
|
public Collider ScopeCollider;
|
|
|
|
private Bounds MoveScope;
|
|
private Vector3 TempPos;
|
|
|
|
void Start() {
|
|
MoveScope = ScopeCollider.bounds;
|
|
}
|
|
|
|
Vector3 IsScope(Vector3 pos) {
|
|
return new Vector3(Mathf.Min(MoveScope.max.x, Mathf.Max(pos.x,MoveScope.min.x)), Mathf.Min(MoveScope.max.y, Mathf.Max(pos.y, MoveScope.min.y)), Mathf.Min(MoveScope.max.z, Mathf.Max(pos.z, MoveScope.min.z)));
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButton(0)) {
|
|
TempPos = transform.position - (transform.forward * Input.GetAxis("Mouse Y") + transform.right * Input.GetAxis("Mouse X")) * PositionSpeed;
|
|
}
|
|
if (Input.GetMouseButton(1))
|
|
{
|
|
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * RotationSpeed);
|
|
}
|
|
transform.position = IsScope(TempPos);
|
|
}
|
|
}
|