
using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FreeCameraController : MonoBehaviour
{
public CinemachineVirtualCamera vcamera;
public Transform target;
public float TopClamp = 85.0f;
public float BottomClamp = -10.0f;
public float Speed = 3.0f;
public bool Invert;
private float cinemachineTargetYaw;
private float cinemachineTargetPitch;
private Cinemachine3rdPersonFollow vcameraFollow;
private float vcameraDistance;
void Start()
{
cinemachineTargetPitch = target.rotation.eulerAngles.x;
cinemachineTargetYaw = target.rotation.eulerAngles.y;
vcameraFollow = vcamera.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
vcameraDistance = vcameraFollow.CameraDistance;
}
void LateUpdate()
{
CameraRotation();
}
private void CameraRotation()
{
var input = new Vector2(Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
bool left = Input.GetMouseButton(0);
bool right = Input.GetMouseButton(1);
if (Invert)
{
bool temp = left;
left = right;
right = temp;
}
if (right)
{
if (input.sqrMagnitude >= 0.01f)
{
cinemachineTargetYaw += input.x * Speed;
cinemachineTargetPitch += input.y * Speed;
}
cinemachineTargetYaw = ClampAngle(cinemachineTargetYaw, float.MinValue, float.MaxValue);
cinemachineTargetPitch = ClampAngle(cinemachineTargetPitch, BottomClamp, TopClamp);
transform.rotation = Quaternion.Euler(0.0f, cinemachineTargetYaw, 0.0f);
target.rotation = Quaternion.Euler(cinemachineTargetPitch, cinemachineTargetYaw, 0.0f);
}
if (left)
{
transform.Translate(-input.x, 0f, input.y);
}
vcameraDistance -= Input.GetAxis("Mouse ScrollWheel") * 10.0f;
vcameraDistance = Mathf.Clamp(vcameraDistance, 2.0f, 20.0f);
vcameraFollow.CameraDistance = Mathf.Lerp(vcameraFollow.CameraDistance, vcameraDistance, Time.deltaTime * 5.0f);
}
private static float ClampAngle(float lfAngle, float lfMin, float lfMax)
{
if (lfAngle < -360f) lfAngle += 360f;
if (lfAngle > 360f) lfAngle -= 360f;
return Mathf.Clamp(lfAngle, lfMin, lfMax);
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75