Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- namespace Controller
- {
- public class Controller_Camera : MonoBehaviour {
- public Transform viewTarget;
- public GameObject go;
- private Vector3 viewTarget_Orig;
- public float distance = 5.0f;
- public float maxDistance = 20f;
- public float minDistance = 0.6f;
- public float rotSpeed = 100f;
- public float panSpeed = 0.25f;
- public float zoomSpeed = 0.25f;
- public int yMinLimit = -80;
- public int yMaxLimit = 80;
- private float xDeg = 0.0f;
- private float yDeg = 0.0f;
- float x = 0;
- float y = 0;
- float z = 0;
- void OnEnable() { init(); }
- public void init()
- {
- if(!viewTarget)
- {
- if(!go)
- go = new GameObject("Cam Target");
- go.transform.position = transform.position + (transform.forward * distance);
- viewTarget = go.transform;
- viewTarget_Orig = viewTarget.position;
- }
- distance = Vector3.Distance(transform.position, viewTarget.position);
- xDeg = Vector3.Angle(Vector3.right, transform.right);
- yDeg = Vector3.Angle(Vector3.up, transform.up);
- }
- private void FixedUpdate()
- {
- transform.LookAt(viewTarget);
- }
- void LateUpdate() {
- x += Input.GetAxis("Mouse X") * rotSpeed * 0.02f;
- y -= Input.GetAxis("Mouse Y") * rotSpeed * 0.02f;
- y = ClampAngle(y, yMinLimit, yMaxLimit);
- var rotation = Quaternion.Euler(y, x, z);
- var position = rotation * new Vector3(0.0f, 0.0f, -distance) + viewTarget.position; //Suspected Cause
- transform.rotation = rotation;
- transform.position = position;
- }
- static float ClampAngle(float angle , float min, float max)
- {
- if (angle < -360)
- angle += 360;
- if (angle > 360)
- angle -= 360;
- return Mathf.Clamp(angle, min, max);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment