Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ublic class CameraController : MonoBehaviour
- {
- public Transform target;
- public Transform cam;
- public Vector3 offsetPos;
- public Vector3 camRotation;
- public float moveSpeed = 5;
- public float turnSpeed = 10;
- public float smoothSpeed = 0.02f;
- Quaternion targetRotation;
- Vector3 targetPos;
- Vector3 targetRot;
- bool smoothRotating = false;
- void Update()
- {
- MoveWithTarget();
- LookAtTarget();
- //if (Input.GetKeyDown(KeyCode.Q) && !smoothRotating)
- //{
- //StartCoroutine("RotateAroundTarget", 45);
- //}
- //if (Input.GetKeyDown(KeyCode.E) && !smoothRotating)
- //{
- //StartCoroutine("RotateAroundTarget", -45);
- //}
- cam.rotation.y = target.rotation.y + 180;
- }
- void MoveWithTarget()
- {
- targetPos = target.position + offsetPos;
- transform.position = Vector3.Lerp(transform.position, targetPos, moveSpeed * Time.deltaTime);
- }
- void LookAtTarget()
- {
- targetRotation = Quaternion.LookRotation(target.position - transform.position);
- transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
- }
- IEnumerator RotateAroundTarget(float angle)
- {
- Vector3 vel = Vector3.zero;
- Vector3 targetOffsetPos = Quaternion.Euler(0, angle, 0) * offsetPos;
- float dist = Vector3.Distance(offsetPos, targetOffsetPos);
- smoothRotating = true;
- while(dist > 0.02f)
- {
- offsetPos = Vector3.SmoothDamp(offsetPos, targetOffsetPos, ref vel, smoothSpeed);
- dist = Vector3.Distance(offsetPos, targetOffsetPos);
- yield return null;
- }
- smoothRotating = false;
- offsetPos = targetOffsetPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement