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

Unity鼠標(biāo)控制3D物體的移動(dòng)、旋轉(zhuǎn)、縮放

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

一、鼠標(biāo)控制3D物體移動(dòng)

1.使用協(xié)程

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

public class ControlMove : MonoBehaviour
{
    Vector3 cubeScreenPos;
    Vector3 offset;

    void Start()
    {
        StartCoroutine(OnMouseDown());//在Start方法中調(diào)用StartCoroutine(要調(diào)用的協(xié)程方法)
    }

    //協(xié)程
    IEnumerator OnMouseDown()
    {
        //1. 得到物體的屏幕坐標(biāo)
        cubeScreenPos = Camera.main.WorldToScreenPoint(transform.position);

        //2. 計(jì)算偏移量
        //鼠標(biāo)的三維坐標(biāo)
        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
        //鼠標(biāo)三維坐標(biāo)轉(zhuǎn)為世界坐標(biāo)
        mousePos = Camera.main.ScreenToWorldPoint(mousePos);
        offset = transform.position - mousePos;

        //3. 物體隨著鼠標(biāo)移動(dòng)
        while (Input.GetMouseButton(0))
        {
            //目前的鼠標(biāo)二維坐標(biāo)轉(zhuǎn)為三維坐標(biāo)
            Vector3 curMousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
            //目前的鼠標(biāo)三維坐標(biāo)轉(zhuǎn)為世界坐標(biāo)
            curMousePos = Camera.main.ScreenToWorldPoint(curMousePos);

            //物體世界位置
            transform.position = curMousePos + offset;
            yield return new WaitForFixedUpdate(); //這個(gè)很重要,循環(huán)執(zhí)行
        }
    }
}

2.鼠標(biāo)左鍵控制物體移動(dòng),鼠標(biāo)碰到物體,物體顏色改變

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

public class TransformModel : MonoBehaviour
{
    //鼠標(biāo)經(jīng)過(guò)時(shí)改變物體顏色
    private Color mouseOverColor = Color.blue;//聲明變量為藍(lán)色
    private Color originalColor;//聲明變量來(lái)存儲(chǔ)本來(lái)顏色

    void Start()
    {
        originalColor = GetComponent<Renderer>().sharedMaterial.color;//開(kāi)始時(shí)得到物體著色
    }

    void OnMouseEnter()
    {
        GetComponent<Renderer>().material.color = mouseOverColor;//當(dāng)鼠標(biāo)滑過(guò)時(shí)改變物體顏色為藍(lán)色
    }

    void OnMouseExit()
    {
        GetComponent<Renderer>().material.color = originalColor;//當(dāng)鼠標(biāo)滑出時(shí)恢復(fù)物體本來(lái)顏色
    }

    IEnumerator OnMouseDown()
    {
        Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三維物體坐標(biāo)轉(zhuǎn)屏幕坐標(biāo)
        //將鼠標(biāo)屏幕坐標(biāo)轉(zhuǎn)為三維坐標(biāo),再計(jì)算物體位置與鼠標(biāo)之間的距離
        var offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        
        while (Input.GetMouseButton(0))
        {
            //將鼠標(biāo)位置二維坐標(biāo)轉(zhuǎn)為三維坐標(biāo)
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            //將鼠標(biāo)轉(zhuǎn)換的三維坐標(biāo)再轉(zhuǎn)換成世界坐標(biāo)+物體與鼠標(biāo)位置的偏移量
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
            transform.position = curPosition;
            yield return new WaitForFixedUpdate();//循環(huán)執(zhí)行
        }
    }
}

二、鼠標(biāo)控制3D物體旋轉(zhuǎn)

1. 控制物體左右旋轉(zhuǎn),上下旋轉(zhuǎn)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformModel : MonoBehaviour
{
    //上下旋轉(zhuǎn)最大角度限制
    public int yMinLimit = -20;
    public int yMaxLimit = 80;
    //旋轉(zhuǎn)速度
    public float xSpeed = 250.0f;//左右旋轉(zhuǎn)速度
    public float ySpeed = 120.0f;//上下旋轉(zhuǎn)速度
    //旋轉(zhuǎn)角度
    private float x = 0.0f;
    private float y = 0.0f;

    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            //Input.GetAxis("MouseX")獲取鼠標(biāo)移動(dòng)的X軸的距離
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            y = ClampAngle(y, yMinLimit, yMaxLimit);
            //歐拉角轉(zhuǎn)化為四元數(shù)
            Quaternion rotation = Quaternion.Euler(y, x, 0);
            transform.rotation = rotation;
        }
    }

    //角度范圍值限定
    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);
    }
}
2. 控制攝像機(jī)以物體為中心旋轉(zhuǎn)

腳本掛載到攝像機(jī)上

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

public class TransformModel : MonoBehaviour
{
    public Transform CenObj;//圍繞的物體
    private Vector3 Rotion_Transform;
    private new Camera camera;
    void Start()
    {
        camera = GetComponent<Camera>();
        Rotion_Transform = CenObj.position;
    }
    void Update()
    {
        Ctrl_Cam_Move();
        Cam_Ctrl_Rotation();
    }
    //鏡頭的遠(yuǎn)離和接近
    public void Ctrl_Cam_Move()
    {
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            transform.Translate(Vector3.forward * 1f);//速度可調(diào)  自行調(diào)整
        }
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            transform.Translate(Vector3.forward * -1f);//速度可調(diào)  自行調(diào)整
        }
    }
    //攝像機(jī)的旋轉(zhuǎn)
    public void Cam_Ctrl_Rotation()
    {
        var mouse_x = Input.GetAxis("Mouse X");//獲取鼠標(biāo)X軸移動(dòng)
        var mouse_y = -Input.GetAxis("Mouse Y");//獲取鼠標(biāo)Y軸移動(dòng)
        if (Input.GetKey(KeyCode.Mouse1))
        {
            transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 5);
            transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5);
        }
    }
}

三、鼠標(biāo)控制3D物體縮放

1. 基于物體本身的Transform的縮放
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransformModel : MonoBehaviour
{
    //縮放比例限制
    public float MinScale = 0.2f;
    public float MaxScale = 3.0f;
    //縮放比例
    private float scale = 1.0f;


    void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            scale += Input.GetAxis("Mouse ScrollWheel");
            scale = Mathf.Clamp(scale, MinScale, MaxScale);
            transform.localScale = new Vector3(scale, scale, scale);
        }
    }
}
2. 基于攝像機(jī)的遠(yuǎn)近的縮放

腳本掛載到攝像機(jī)上文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-513117.html

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

public class TransformModel : MonoBehaviour
{
    void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //鼠標(biāo)滾動(dòng)滑輪 值就會(huì)變化
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                //范圍值限定
                if (Camera.main.fieldOfView <= 100)//攝像機(jī)采用Perspective透視
                    Camera.main.fieldOfView += 2;
                if (Camera.main.orthographicSize <= 20)//攝像機(jī)采用Orthograpic正交
                    Camera.main.orthographicSize += 0.5F;
            }
            //Zoom in  
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                //范圍值限定
                if (Camera.main.fieldOfView > 2)//攝像機(jī)采用Perspective透視
                    Camera.main.fieldOfView -= 2;
                if (Camera.main.orthographicSize >= 1)//攝像機(jī)采用Orthograpic正交
                    Camera.main.orthographicSize -= 0.5F;
            }
        }
    }
}

到了這里,關(guān)于Unity鼠標(biāo)控制3D物體的移動(dòng)、旋轉(zhuǎ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攝像機(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)
  • Unity Dotween插件物體、ui的移動(dòng) 旋轉(zhuǎn) 縮放效果

    Unity Dotween插件物體、ui的移動(dòng) 旋轉(zhuǎn) 縮放效果 #腳本應(yīng)用命名空間 using DG.Tweening; ##讓游戲物體平移 //參數(shù)一:要移動(dòng)到的位置 //參數(shù)二:動(dòng)畫(huà)完成要多少秒 transform.DOMove(new Vector3(1,3, 8.08f),5.5f); ##讓游戲物體旋轉(zhuǎn) //參數(shù)一:要旋轉(zhuǎn)成的樣子的坐標(biāo) //參數(shù)二:旋轉(zhuǎn)動(dòng)畫(huà)多少秒完成 trans

    2024年02月01日
    瀏覽(25)
  • 【Unity】攝像機(jī)跟隨鼠標(biāo)移動(dòng)以物體為中心旋轉(zhuǎn) 物體根據(jù)視線方向移動(dòng)

    【Unity】攝像機(jī)跟隨鼠標(biāo)移動(dòng)以物體為中心旋轉(zhuǎn) 物體根據(jù)視線方向移動(dòng)

    描述 實(shí)現(xiàn)攝像機(jī)根據(jù)鼠標(biāo)移動(dòng)跟隨物體旋轉(zhuǎn),以攝像機(jī)前物體為中心,攝像機(jī)圍繞物體旋轉(zhuǎn),并使攝像機(jī)時(shí)刻指向物體 實(shí)現(xiàn)效果 Unity 組件設(shè)置 Camera 組件設(shè)置 Body 組件設(shè)置 實(shí)現(xiàn)代碼 CameraRotateMove.cs 攝像機(jī)跟隨和旋轉(zhuǎn) move_better.cs 物體根據(jù)按鍵移動(dòng)

    2024年02月08日
    瀏覽(41)
  • unity3d---移動(dòng)、縮放、旋轉(zhuǎn)

    unity3d---移動(dòng)、縮放、旋轉(zhuǎn)

    目錄 1.示意圖 2.觸屏移動(dòng)與縮放+鍵盤移動(dòng)、旋轉(zhuǎn)與縮放+鼠標(biāo)移動(dòng)旋轉(zhuǎn)與縮放 1.示意圖 ? 2.觸屏移動(dòng)與縮放+鍵盤移動(dòng)、旋轉(zhuǎn)與縮放+鼠標(biāo)移動(dòng)旋轉(zhuǎn)與縮放

    2024年02月06日
    瀏覽(95)
  • Unity控制相機(jī)旋轉(zhuǎn)、移動(dòng)、縮放等功能

    提示: 該腳本允許你以指定的速度和角度圍繞模型進(jìn)行相機(jī)旋轉(zhuǎn),并可以控制相機(jī)的移動(dòng)和縮放 將該腳本添加到一個(gè)游戲?qū)ο笊希⑵渥鳛橹鲾z像機(jī)。 1、在Unity編輯器中,你可以在腳本的參數(shù)變量部分調(diào)整相機(jī)的速度、縮放和移動(dòng)等設(shè)置。根據(jù)需求,修改各個(gè)參數(shù)的值。

    2024年02月04日
    瀏覽(44)
  • 【Unity3D】游戲物體操作 ③ ( 旋轉(zhuǎn)操作 | 旋轉(zhuǎn)工具 | 基本旋轉(zhuǎn) | 設(shè)置旋轉(zhuǎn)屬性 | 增量旋轉(zhuǎn) | 縮放操作 | 軸向縮放 | 整體縮放 | 操作工具切換 | 操作模式切換 )

    【Unity3D】游戲物體操作 ③ ( 旋轉(zhuǎn)操作 | 旋轉(zhuǎn)工具 | 基本旋轉(zhuǎn) | 設(shè)置旋轉(zhuǎn)屬性 | 增量旋轉(zhuǎn) | 縮放操作 | 軸向縮放 | 整體縮放 | 操作工具切換 | 操作模式切換 )

    選中 Scene 場(chǎng)景 中的 游戲物體 GameObject , 點(diǎn)擊 工具欄 中的 轉(zhuǎn)換工具 , 此時(shí)在該 游戲物體 會(huì)被 4 個(gè) 圓圈 環(huán)繞 ; 紅圈 : 拖動(dòng)該圈 , 繞 X 軸旋轉(zhuǎn) ; 綠圈 : 拖動(dòng)該圈 , 繞 Y 軸旋轉(zhuǎn) ; 藍(lán)圈 : 拖動(dòng)該圈 , 繞 Z 軸旋轉(zhuǎn) ; 最外層還有一個(gè) 白圈 ; 鼠標(biāo)左鍵按住旋轉(zhuǎn) : 在 Unity 旋轉(zhuǎn) 游戲物體

    2023年04月08日
    瀏覽(168)
  • unity3D 鼠標(biāo)滾輪實(shí)現(xiàn)物體的大小縮放

    鼠標(biāo)滾輪響應(yīng)函數(shù)是Input.GetAxis(\\\"Mouse ScrollWheel\\\"),函數(shù)返回值類型是float,向前滾是返回正數(shù),向后滾是返回負(fù)數(shù),且鼠標(biāo)滾輪滑動(dòng)單次函數(shù)返回值為0.1 利用返回值修改模型transform.localscale,實(shí)現(xiàn)模型縮放 鼠標(biāo)滾輪一直向后滾,會(huì)看見(jiàn)模型逐漸變小,當(dāng)變到很小到消失的時(shí)候,

    2024年02月08日
    瀏覽(96)
  • 7. unity腳本控制物體運(yùn)動(dòng)(轉(zhuǎn)向、移動(dòng)、旋轉(zhuǎn))

    1. 移動(dòng)物體: 在界面中添加一個(gè)物體,并給這個(gè)物體添加一個(gè)腳本文件,在腳本文件當(dāng)中如果想控制物體運(yùn)動(dòng),就需要在 unity 每一幀更新的時(shí)候,給物體的坐標(biāo)值重新賦值,這樣就能按照幀更新速率實(shí)時(shí)的更改物體的位置,實(shí)現(xiàn)移動(dòng)效果。 位置更新的代碼應(yīng)該寫在***update

    2023年04月12日
    瀏覽(42)
  • 【Unity入門】Input.GetAxis(““)控制物體移動(dòng)、旋轉(zhuǎn)

    Input.GetAxis(“”) 是 Unity 引擎中的一個(gè)方法,用于獲取游戲玩家在 鍵盤 或 游戲手柄 上輸入的某個(gè)軸(Axis)的值。這里的 “” 是一個(gè)字符串參數(shù),表示要獲取的軸的名稱。 在 Unity 中,有多種軸類型,如 “Horizontal”(水平軸)、“Vertical”(垂直軸)、“Mouse X”(鼠標(biāo)水平

    2024年01月17日
    瀏覽(21)
  • 【Unity腳本開(kāi)源】記錄鼠標(biāo)按下的位置和移動(dòng)的距離來(lái)進(jìn)行物體的旋轉(zhuǎn),并在鼠標(biāo)釋放后將物體恢復(fù)到初始旋轉(zhuǎn)位置

    ??作者:白日參商 ???♂?個(gè)人主頁(yè):白日參商主頁(yè) ??堅(jiān)持分析平時(shí)學(xué)習(xí)到的項(xiàng)目以及學(xué)習(xí)到的軟件開(kāi)發(fā)知識(shí),和大家一起努力呀!?。?????加油! 加油! 加油! 加油 ??歡迎評(píng)論 ??點(diǎn)贊???? 收藏 ??加關(guān)注+! 提針對(duì)這個(gè)需求,以下是示例腳本代碼: ??作者

    2024年02月12日
    瀏覽(22)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包