最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動并限制角度

 更新時(shí)間:2020年05月15日 09:27:41   作者:Studious_S  
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動并限制角度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動并限制角度的具體代碼,供大家參考,具體內(nèi)容如下

攝像機(jī)鏡頭跟隨鼠標(biāo)移動,并限制上下左右的移動角度

public class ViewFromCream : MonoBehaviour 
{
  public int speed=5;
  public Vector3 vect;
  
  private float xcream;
  private float ycream;

  public void Update()
  {
    CreamView();
  }

  private void CreamView()
  {
    float x = Input.GetAxis("Mouse X");
    float y = Input.GetAxis("Mouse Y");
    if (x!=0||y!=0)
    {
      LimitAngle(60);
      LimitAngleUandD(60);
      this.transform.Rotate(-y * speed, 0, 0);
      this.transform.Rotate(0, x * speed, 0, Space.World);
    }
  }

  /// <summary>
  /// 限制相機(jī)左右視角的角度
  /// </summary>
  /// <param name="angle">角度</param>
  private void LimitAngle(float angle)
  {
    vect = this.transform.eulerAngles;
    //當(dāng)前相機(jī)x軸旋轉(zhuǎn)的角度(0~360)
    xcream = IsPosNum(vect.x);    
    if (xcream > angle)
      this.transform.rotation = Quaternion.Euler(angle,vect.y,0);
    else if (xcream < -angle)
      this.transform.rotation = Quaternion.Euler(-angle, vect.y, 0);
  }
  /// <summary>
  /// 限制相機(jī)上下視角的角度
  /// </summary>
  /// <param name="angle"></param>
  private void LimitAngleUandD(float angle)
  {
    vect = this.transform.eulerAngles;
    //當(dāng)前相機(jī)y軸旋轉(zhuǎn)的角度(0~360)
    ycream = IsPosNum(vect.y);
    if (ycream > angle)
      this.transform.rotation = Quaternion.Euler(vect.x, angle, 0);
    else if (ycream < -angle)
      this.transform.rotation = Quaternion.Euler(vect.x, -angle, 0);
  }
  /// <summary>
  /// 將角度轉(zhuǎn)換為-180~180的角度
  /// </summary>
  /// <param name="x"></param>
  /// <returns></returns>
  private float IsPosNum(float x)
  {
    x -= 180;
    if (x < 0)
      return x + 180;
    else return x - 180;   
  }
}

對IsPosNum方法進(jìn)行說明

之所以要將獲取的歐拉角轉(zhuǎn)換為-180°-180°之間,是因?yàn)樵讷@取eulerAngle中,x軸和y軸的值只有0-360,而沒有負(fù)數(shù),那么這將會復(fù)雜化我們角度的判斷,如限制左右角度為-60-60之間,那么我們就要判斷角度是否超過300度或是超過60度, 顯然超過300度的角度必定超過60度,那么就需要另外加條件進(jìn)行判斷;因此對獲取的值進(jìn)行轉(zhuǎn)化更為方便!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

南投市| 印江| 章丘市| 河北区| 三亚市| 高淳县| 师宗县| 开原市| 万山特区| 南康市| 米易县| 铁岭县| 时尚| 郴州市| 拜城县| 泾川县| 大姚县| 花莲县| 宿州市| 凌源市| 吉木乃县| 临清市| 株洲县| 广水市| 淳安县| 依兰县| 镇原县| 秦皇岛市| SHOW| 垦利县| 金华市| 门源| 嘉义县| 德化县| 射洪县| 澜沧| 榕江县| 玛纳斯县| 德化县| 岳池县| 黑河市|