欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實(shí)現(xiàn)鼠標(biāo)點(diǎn)2D轉(zhuǎn)3D進(jìn)行旋轉(zhuǎn)

 更新時間:2020年04月17日 11:53:20   作者:木小星  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)鼠標(biāo)點(diǎn)2D轉(zhuǎn)3D進(jìn)行旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity實(shí)現(xiàn)鼠標(biāo)點(diǎn)2D轉(zhuǎn)3D進(jìn)行旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下

代碼如下:

using UnityEngine;

public class GunFollowMouse : MonoBehaviour {
  public RectTransform UGUICanvas;
  public Camera mainCamera;
  //攝像機(jī)旋轉(zhuǎn)的緩動速率
  private float rotateSpeed = 5;
  void Start () {
 
 }
 
 void Update () {
    //定義一個世界坐標(biāo)的鼠標(biāo)點(diǎn)
    Vector3 mousePos;
    //獲取當(dāng)前canvas下鼠標(biāo)的二維坐標(biāo)點(diǎn) 轉(zhuǎn)化為三維 out出來
    RectTransformUtility.ScreenPointToWorldPointInRectangle(UGUICanvas,
      new Vector2(Input.mousePosition.x, Input.mousePosition.y),
      mainCamera, out mousePos
      );
    //炮臺的旋轉(zhuǎn)角度
    float angle;
    //向量dirMouse為鼠標(biāo)的向量減去槍的起始向量得到槍到鼠標(biāo)位置的方向向量
    Vector3 dirMouse = mousePos - transform.position;
    angle = Vector3.Angle(dirMouse, Vector3.up);//直接得到兩個向量之間的夾角,這個角度是沒有正負(fù)的
    if (mousePos.x > transform.position.x)
    {
      angle = - angle;
    }
    transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, angle)
      , Time.deltaTime * rotateSpeed);
 }
}

旋轉(zhuǎn)效果如下圖:

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

相關(guān)文章

最新評論