Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class RCameraControls : MonoBehaviour {
- public Transform target;
- public Vector3 offset;
- public bool useOffsetValues;
- public float rotateSpeed;
- public Transform pivot;
- public float maxViewAngle;
- public float minViewAngle;
- public bool InvertY;
- // Use this for initialization
- void Start () {
- if (!useOffsetValues)
- {
- offset = target.position - transform.position;
- }
- pivot.transform.position = target.transform.position;
- pivot.transform.parent = target.transform;
- Cursor.lockState = CursorLockMode.Locked;
- }
- // Update is called once per frame
- void LateUpdate () {
- //Поворот камеры по иксу
- float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
- target.Rotate(0, horizontal, 0);
- // поворот по игрику
- float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
- pivot.Rotate(-vertical, 0, 0);
- if (InvertY)
- {
- pivot.Rotate(vertical, 0, 0);
- }
- else
- {
- pivot.Rotate(-vertical, 0, 0);
- }
- //Верхний и нижний угол камеры
- if(pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
- {
- pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
- }
- if(pivot.rotation.eulerAngles.x > 180 && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
- {
- pivot.rotation = Quaternion.Euler(360f + minViewAngle, 0, 0);
- }
- //движение камеры в зависимости от позиции
- float desiredYAngle = target.eulerAngles.y;
- float desiredXAngle = pivot.eulerAngles.x;
- Quaternion rotation = Quaternion.Euler(desiredXAngle , desiredYAngle, 0);
- transform.position = target.position - ( rotation * offset);
- //изминение позиции
- if(transform.position.y < target.position.y)
- {
- transform.position = new Vector3(transform.position.x, target.position.y, transform.position.z);
- }
- transform.LookAt(target);
- }
- }
Add Comment
Please, Sign In to add comment