Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class mouseOrbit : MonoBehaviour {
- public Transform target;
- public float distance = 5.0f;
- public float xSpeed = 120.0f;
- public float ySpeed = 120.0f;
- public float ySpeedInverted = -120f;
- public float yMinLimit = -20f;
- public float yMaxLimit = 80f;
- public float distanceMin = .5f;
- public float distanceMax = 15f;
- public bool pause { get; set; }
- public bool invertedMl { get; set; }
- public Vector3 angles;
- public float x = 0.0f;
- public float y = 0.0f;
- private Rigidbody rb;
- private void Start()
- {
- pause = false;
- invertedMl = false;
- angles = transform.eulerAngles;
- x = angles.y;
- y = angles.x;
- rb = GetComponent<Rigidbody>();
- if (rb != null)
- {
- rb.freezeRotation = true;
- }
- }
- private void LateUpdate()
- {
- if(target != null)
- {
- print("Orbit has target");
- if (!pause)
- {
- print("Game is not paused");
- if (!invertedMl)
- {
- print("uninverted ran");
- x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
- y -= Input.GetAxis("Mouse Y") * ySpeedInverted * 0.02f;
- y = ClampAngle(y, yMinLimit, yMaxLimit);
- Quaternion rotation = Quaternion.Euler(y, x, 0);
- distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
- RaycastHit hit;
- if (Physics.Linecast(target.position, transform.position, out hit))
- {
- distance -= hit.distance;
- }
- Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
- Vector3 position = rotation * negDistance + target.position;
- transform.rotation = rotation;
- transform.position = position;
- }
- else
- {
- print("inverted ran");
- x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
- y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
- y = ClampAngle(y, yMinLimit, yMaxLimit);
- Quaternion rotation = Quaternion.Euler(y, x, 0);
- distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
- RaycastHit hit;
- if (Physics.Linecast(target.position, transform.position, out hit))
- {
- distance -= hit.distance;
- }
- Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
- Vector3 position = rotation * negDistance + target.position;
- transform.rotation = rotation;
- transform.position = position;
- }
- }
- }
- }
- private static float ClampAngle(float angle, float min, float max)
- {
- if (angle < -360F)
- angle += 360F;
- if (angle > 360F)
- angle -= 360F;
- return Mathf.Clamp(angle, min, max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement