国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Unity中鼠標(biāo)控制3D物體進(jìn)行拖拽

這篇具有很好參考價(jià)值的文章主要介紹了Unity中鼠標(biāo)控制3D物體進(jìn)行拖拽。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

軸方向上的移動(dòng)

拖拽

1 、獲取3D物體在世界坐標(biāo)的位置轉(zhuǎn)換屏幕坐標(biāo)

 Camera.main.WorldToScreenPoint(dragBeforeGameObjPos)

2、鼠標(biāo)在屏幕的坐標(biāo)與物體在屏幕的坐標(biāo)Z軸進(jìn)行擬合

Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z))

3、獲取3D物體與擬合出來(lái)的坐標(biāo)的偏移量

 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));

4、通過(guò)向量投影得到移動(dòng)坐標(biāo)

 Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);

5、上干貨,附帶手指控制,手指控制邏輯同鼠標(biāo)一樣

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PointAxis : MonoBehaviour
{
    public bool IsBeSelected;
    [SerializeField]
    private PointAxis_Type curPAType;

    private bool isMouseDrag = false;
    private Vector3 screenPosition;
    private Vector3 offset;
    private Vector3 dragBeforeGameObjPos;
    void Start()
    {

        //parentTransform = transform.parent;
        //mr = GetComponent<MeshRenderer>();
        switch (curPAType)
        {
            case PointAxis_Type.Axis_X:
                //colorNormal = ColorNormalX;
                break;
            case PointAxis_Type.Axis_Y:
                //colorNormal = ColorNormalY;
                break;
            case PointAxis_Type.Axis_Z:
                //colorNormal = ColorNormalZ;
                break;
        }

    }

    // Update is called once per frame
    void Update()
    {
#if (UNITY_ANDROID || UNITY_IPHONE)
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
         {
            dragBeforeGameObjPos = transform.position;
            screenPosition = Camera.main.WorldToScreenPoint(dragBeforeGameObjPos);
            offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z));
            isMouseDrag = true;
        }


         if (Input.touchCount > 0 &&Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            if (isMouseDrag && IsBeSelected)
            {
                //Debug.Log("開(kāi)始拖拽了");
                Vector3 currentScreenSpace = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
               // float tempLength = Vector3.Distance(currentPosition, transform.position);
                Vector3 tempPos = Vector3.zero;
                switch (curPAType)
                {
                    case PointAxis_Type.Axis_X:
                        tempPos = Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Y:
                        tempPos = Vector3.Project(currentPosition, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Z:
                        tempPos = Vector3.Project(currentPosition, transform.forward) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right);
                        break;
                }
                transform.position = tempPos;
            }
        }
        

         if(Input.GetTouch(0).phase == TouchPhase.Ended)
            isMouseDrag = false;
#else

        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            dragBeforeGameObjPos = transform.position;
            screenPosition = Camera.main.WorldToScreenPoint(dragBeforeGameObjPos);
            offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
            isMouseDrag = true;
        }
        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (isMouseDrag && IsBeSelected)
            {
                Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
                Vector3 tempPos = Vector3.zero;
                switch (curPAType)
                {
                    case PointAxis_Type.Axis_X:
                        tempPos = Vector3.Project(currentPosition, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Y:
                        tempPos = Vector3.Project(currentPosition, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right) + Vector3.Project(dragBeforeGameObjPos, transform.forward);
                        break;
                    case PointAxis_Type.Axis_Z:
                        tempPos = Vector3.Project(currentPosition, transform.forward) + Vector3.Project(dragBeforeGameObjPos, transform.up) + Vector3.Project(dragBeforeGameObjPos, transform.right);
                        break;
                }
                transform.position = tempPos;
            }
        }
        if (Input.GetKeyUp(KeyCode.Mouse0))
            isMouseDrag = false;
#endif

    }
}
public enum PointAxis_Type
{
    Axis_X, Axis_Y, Axis_Z
}

自由拖拽移動(dòng)

自由拖拽

自由拖拽同上不同的是 獲取3D物體與擬合出來(lái)的坐標(biāo)的加上偏移量限制Y軸坐標(biāo)就搞定了,

話不多說(shuō)上干貨文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-843531.html

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PointEntity : MonoBehaviour
{

    public bool IsBeSelected = false;
    private bool isMouseDrag = false;
    private Vector3 screenPosition;
    private Vector3 offset;
    
    void Start()
    {
    }
    
    void Update()
    {
#if (UNITY_ANDROID || UNITY_IPHONE)
          if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
         {
             screenPosition = Camera.main.WorldToScreenPoint(gameObject.transform.position);
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z));
            isMouseDrag = true;
        }


         if (Input.touchCount > 0 &&Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            if (isMouseDrag && IsBeSelected)
            {
                //Debug.Log("開(kāi)始拖拽了");
                Vector3 currentScreenSpace = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
                currentPosition = new Vector3(currentPosition.x, transform.position.y, currentPosition.z);
                gameObject.transform.position = currentPosition;
            }
        }
        
         if(Input.GetTouch(0).phase == TouchPhase.Ended)
            isMouseDrag = false;
#else
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            screenPosition = Camera.main.WorldToScreenPoint(gameObject.transform.position);
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
            isMouseDrag = true;
        }
        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (isMouseDrag && IsBeSelected)
            {
                //Debug.Log("開(kāi)始拖拽了");
                Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
                currentPosition = new Vector3(currentPosition.x, transform.position.y, currentPosition.z);
                gameObject.transform.position = currentPosition;
            }
        }
        if (Input.GetKeyUp(KeyCode.Mouse0))
        {
            isMouseDrag = false;
        }
#endif
    }
}

到了這里,關(guān)于Unity中鼠標(biāo)控制3D物體進(jìn)行拖拽的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Unity3D實(shí)現(xiàn)鼠標(biāo)懸浮UI或物體上顯示文字信息

    Unity3D實(shí)現(xiàn)鼠標(biāo)懸浮UI或物體上顯示文字信息

    Unity工具 大家好,我是心疼你的一切,不定時(shí)更新Unity開(kāi)發(fā)技巧,覺(jué)得有用記得一鍵三連哦。 本篇文章實(shí)現(xiàn)一個(gè)鼠標(biāo)懸浮在ui或者物體上顯示文字的功能 unity鼠標(biāo)懸浮ui顯示文字 鼠標(biāo)進(jìn)入U(xiǎn)I執(zhí)行的 鼠標(biāo)離開(kāi)UI執(zhí)行的 鼠標(biāo)在ui里滑動(dòng)執(zhí)行的 1. 2. 搭建比較簡(jiǎn)單,各位順便搭建吧。

    2024年02月04日
    瀏覽(492)
  • Unity3D 控制物體移動(dòng)且自動(dòng)旋轉(zhuǎn)

    Unity3D 控制物體移動(dòng)且自動(dòng)旋轉(zhuǎn)

    直接上代碼(改腳本掛載到游戲物體上) 第三人稱攝像機(jī)移動(dòng)

    2024年02月15日
    瀏覽(98)
  • Unity實(shí)現(xiàn)鼠標(biāo)拖拽多物體(拖拽單物體的拓展)

    Unity實(shí)現(xiàn)鼠標(biāo)拖拽多物體(拖拽單物體的拓展)

    學(xué)習(xí)了B站UP主OneCredit【Unity快速教學(xué)】鼠標(biāo)拖曳甩動(dòng)物件BV1qK4y1d7iZ的教學(xué)視頻后 拓展了一下功能,實(shí)現(xiàn)多個(gè)物體也可以拖拽,互不受影響 主要是做了一個(gè)檢測(cè),在鼠標(biāo)上物體才能被拖拽 目錄 bool Drag 拖拽物體的實(shí)現(xiàn) 解決攝像機(jī)視角的影響 綁定剛體,寫好需要用到的變量 Dr

    2024年03月24日
    瀏覽(16)
  • 【Unity3D】Unity 腳本 ③ ( C# 腳本的執(zhí)行入口函數(shù) | 獲取當(dāng)前游戲物體及物體名稱 | 獲取游戲物體的 Transform 組件數(shù)據(jù) | UnityEngine 命名空間簡(jiǎn)介 )

    【Unity3D】Unity 腳本 ③ ( C# 腳本的執(zhí)行入口函數(shù) | 獲取當(dāng)前游戲物體及物體名稱 | 獲取游戲物體的 Transform 組件數(shù)據(jù) | UnityEngine 命名空間簡(jiǎn)介 )

    在 C# 腳本中控制 游戲物體 GameObject 運(yùn)動(dòng) , 要先獲取該物體 , 然后 修改其 Transform 組件的屬性 ; 在 游戲開(kāi)始運(yùn)行后 , 會(huì)自動(dòng)執(zhí)行 游戲物體 GameObject 上的 C# 組件代碼 , 程序入口是 MonoBehaviour#Start() 函數(shù) ; 在 C# 腳本中 , 主要的內(nèi)容都在 Start() 函數(shù) 中實(shí)現(xiàn) ; 在 C# 腳本中 , 游戲物體

    2023年04月12日
    瀏覽(110)
  • Unity3D:當(dāng)頻繁隱藏和顯示游戲物體時(shí),最優(yōu)的處理方式

    首先說(shuō)明一下處理的方法一般一共有3種 1.SetActive顯示和隱藏物體【不推薦】 優(yōu):停止了Update和LateUpdate的性能消耗 劣:每一次顯示會(huì)調(diào)用OnEnable,每一次隱藏會(huì)調(diào)用OnDisable 2.設(shè)置物體的位置,使物體移出攝像機(jī)的視野【不推薦】 優(yōu):相比較SetActive來(lái)說(shuō),沒(méi)什么性能消耗 劣:

    2024年02月12日
    瀏覽(44)
  • 控制renderQueue解決NGUI與Unity3D物體渲染順序問(wèn)題

    NGUI與Unity3D物體渲染順序問(wèn)題,做過(guò)UI的各位應(yīng)該都遇到過(guò)。主要指的是UI與Unity制作的特效、3D人物等一同顯示時(shí)的層次問(wèn)題。 由于UI與特效等都是以transparent方式渲染,而Unity與NGUI在管理同是透明物體的render queue時(shí)實(shí)際上互相沒(méi)有感知,于是引出排序問(wèn)題。現(xiàn)在介紹以render

    2024年02月13日
    瀏覽(27)
  • Unity3d用Animator實(shí)現(xiàn)鼠標(biāo)控制多個(gè)門開(kāi)關(guān)

    Unity3d用Animator實(shí)現(xiàn)鼠標(biāo)控制多個(gè)門開(kāi)關(guān)

    本人Unity3d小白,目前正在學(xué)習(xí)U3d,這個(gè)功能想用Animator實(shí)現(xiàn),從Key幀到寫腳本摸索了三天時(shí)間,走了不少?gòu)澛?,搜了不少?shí)例,有版本改版原因也有的是在Unity中搭建的小場(chǎng)景,不涉及父物體,總之開(kāi)始做的功能沒(méi)有實(shí)現(xiàn)?,F(xiàn)在把我實(shí)現(xiàn)的方法記錄下來(lái),算是對(duì)個(gè)功能的總結(jié)

    2024年02月13日
    瀏覽(28)
  • Unity3D通過(guò)代碼修改RGB值來(lái)控制UI與物體的Color顏色

    Unity3D通過(guò)代碼修改RGB值來(lái)控制UI與物體的Color顏色

    在實(shí)際應(yīng)用過(guò)程中,經(jīng)常有需要將某一物體顏色修改成特定顏色的情況,此時(shí)Color中自帶的顏色就不夠用了,此時(shí)我們就需要通過(guò)用代碼修改RGB值來(lái)控制顏色。 下面以修改TextMeshProUGUI的字體顏色為例 這樣直接運(yùn)行,無(wú)論Color后的括號(hào)里是什么樣的參數(shù),字體顏色大概率為白色

    2024年02月11日
    瀏覽(59)
  • Unity3D攝像機(jī),鍵盤控制前后左右上下移動(dòng),鼠標(biāo)控制旋轉(zhuǎn)、放縮

    Unity3D中運(yùn)行場(chǎng)景時(shí),實(shí)現(xiàn)攝像機(jī)的前、后、左、右、上、下,以及鼠標(biāo)滾輪的放縮,鼠標(biāo)右鍵的旋轉(zhuǎn)操作。親測(cè)有效,可供參考。 按鍵功能介紹:W——前;S——后;A——左;D——右;Q——下降;E——上升;鼠標(biāo)右鍵——旋轉(zhuǎn);鼠標(biāo)滾輪——放縮。 Tourcamera腳本需要掛在攝

    2024年02月11日
    瀏覽(26)
  • unity2d里實(shí)現(xiàn)鼠標(biāo)拖拽物體的功能

    在 Unity 中實(shí)現(xiàn)鼠標(biāo)拖拽物體的功能需要使用到 Unity 的 Physics 系統(tǒng)。 要實(shí)現(xiàn)鼠標(biāo)拖拽物體,你需要在場(chǎng)景中添加以下內(nèi)容: 一個(gè) Rigidbody 2D 組件,用于控制物體的運(yùn)動(dòng)。 一個(gè) Box Collider 2D 組件,用于檢測(cè)鼠標(biāo)與物體的碰撞。 一個(gè)腳本,用于監(jiān)聽(tīng)鼠標(biāo)的輸入,并在鼠標(biāo)按下時(shí)拖

    2024年02月11日
    瀏覽(20)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包