using UnityEngine;
using UnityEngine;
using System.Collections;
public class DragMouseOrbit : MonoBehaviour
{
public Transform target;
public float distance = 2.0f;
public float xSpeed = 20.0f;
public float ySpeed = 20.0f;
public float yMinLimit = -90f;
public float yMaxLimit = 90f;
public float distanceMin = 10f;
public float distanceMax = 10f;
public float smoothTime = 2f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
//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;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
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);
}
}
from https://answers.unity.com/questions/1257281/how-to-rotate-camera-orbit-around-a-game-object-on.html
'Game development > Unity' 카테고리의 다른 글
유니티에서 UI Button을 계속 누르고 있는 상태를 Check하는 법 (0) | 2018.03.16 |
---|---|
유니티에서 2D 캐릭터 에니메이션 만들 때 좌우 대칭 손쉽게 하는 법 (0) | 2018.02.20 |
유니티 3D Grid Tile + Object Placement + Wall Constructer (0) | 2018.01.24 |
유니티 타일맵에서 일반적인 Vector3 position을 타일 맵 위에서 Vector3Int로 바꿔주는 코드 (0) | 2018.01.16 |
애드몹과 구글 플레이 게임 서비스간의 충돌 해결법 (1) | 2017.12.08 |