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 CameraMovement : MonoBehaviour
- {
- public Transform target;
- public Transform parent;
- public float distance = 5.0f;
- public float xSpeed = 120.0f;
- public float ySpeed = 120.0f;
- public float yMinLimit = -20f;
- public float yMaxLimit = 80f;
- public float distanceMin = .5f;
- public float distanceMax = 15f;
- public bool isSwitchingTarget;
- float x = 0.0f;
- float y = 0.0f;
- Vector3 position;
- // Use this for initialization
- void Start()
- {
- isSwitchingTarget = false;
- Vector3 angles = transform.eulerAngles;
- x = angles.y;
- y = angles.x;
- }
- private void Update()
- {
- if (Input.GetMouseButton(0))
- {
- ChangeTargets(2f);
- }
- }
- void LateUpdate()
- {
- if (target)
- {
- if (Input.GetMouseButton(1))
- {
- x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
- y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
- y = ClampAngle(y, yMinLimit, yMaxLimit);
- }
- if (!isSwitchingTarget)
- {
- Quaternion rotation = Quaternion.Euler(y, x, 0);
- //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
- Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
- position = rotation * negDistance + target.position;
- transform.rotation = rotation;
- transform.position = position;
- }
- }
- }
- public 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);
- }
- public void ChangeTargets(float time)
- {
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit))
- {
- Debug.Log(hit.collider.name);
- target = null;
- Vector3 newpos = hit.collider.transform.position;
- target = hit.collider.transform;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement