unity 實現攝像機繞某點旋轉一周

在Update函數中執行:

if (turnAround)
  {
    playerCamera.RotateAround(transform.localPosition, transform.up, Time.deltaTime * rotateSpeed);
    degree += Time.deltaTime * rotateSpeed;
    if (degree >= 360)
    {
      turnAround = false;        
      degree = 0;
     }
   }

補充:unity 攝像機圍繞某個物體進行旋轉放大縮小

腳本通過以一個物體為中心點

來控制攝像機圍繞物體旋轉縮放 ,腳本掛在攝像機上即可,target是需要觀看的物體

public Transform Camera2;
public GameObject target;
private float dis;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10;                   //移動速度
public float yMinLimit = -50, yMaxLimit = 50;                           //攝像機的Y軸移動最小最大限制
public float distance = 7, minDistance = 2, maxDistance = 30;           //攝像機與目標物體的距離
public bool needDamping = true;                                         //阻尼默認開啟
float damping = 5.0f;                                                   //默認阻尼為5.0F
public float x = 0.0f;                                                  //X軸
public float y = 0.0f;                                                  //Y軸
// Use this for initialization
void Start() {
    instance = this;
    Camr = Camera2Rotation.Close;
    Vector3 angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;  
}
private void Update()
{
}
void LateUpdate()
{
            //使用按下鼠標左鍵移動物體
            if (Input.GetMouseButton(1))
            {
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                y = ClampAngle(y, yMinLimit, yMaxLimit);
            }
            distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
            distance = Mathf.Clamp(distance, minDistance, maxDistance);
            Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
            Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
            Vector3 position = rotation * disVector + target.transform.position;
            //adjust the camera
            if (needDamping)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
            }
            else
            {
                transform.rotation = rotation;
                transform.position = position;
            }   
}
/// <summary>
/// 旋轉角度的控制
/// </summary>
/// <param name="angle">旋轉的角度</param>
/// <param name="min">最小角度</param>
/// <param name="max">最大角度</param>
/// <returns></returns>
static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp(angle, min, max);
}
void MoveCameraToTarget()
{
    if (target != null)
    {
        Camera2.LookAt(target.transform);
        Camera2.position = Vector3.MoveTowards(Camera2.position, target.transform.position, 5 * Time.deltaTime);
        dis = Vector3.Distance(Camera2.transform.position, target.transform.position);
        if (dis < 1.5f)
        {
            Camr = Camera2Rotation.Open;
            CancelInvoke();
        }
    }
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀:

    None Found