首页 > 其他分享 >Unity 操作3D模型旋转+放大+移动

Unity 操作3D模型旋转+放大+移动

时间:2023-03-14 16:58:27浏览次数:41  
标签:鼠标 Vector3 transform 旋转 Unity using Input public 3D

一、鼠标控制3D物体移动

1.使用协程

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

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

    void Start()
    {
        StartCoroutine(OnMouseDown());//在Start方法中调用StartCoroutine(要调用的协程方法)
    }

    //协程
    IEnumerator onm ouseDown()
    {
        //1. 得到物体的屏幕坐标
        cubeScreenPos = Camera.main.WorldToScreenPoint(transform.position);

        //2. 计算偏移量
        //鼠标的三维坐标
        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
        //鼠标三维坐标转为世界坐标
        mousePos = Camera.main.ScreenToWorldPoint(mousePos);
        offset = transform.position - mousePos;

        //3. 物体随着鼠标移动
        while (Input.GetMouseButton(0))
        {
            //目前的鼠标二维坐标转为三维坐标
            Vector3 curMousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cubeScreenPos.z);
            //目前的鼠标三维坐标转为世界坐标
            curMousePos = Camera.main.ScreenToWorldPoint(curMousePos);

            //物体世界位置
            transform.position = curMousePos + offset;
            yield return new WaitForFixedUpdate(); //这个很重要,循环执行
        }
    }
}

 

2.鼠标左键控制物体移动,鼠标碰到物体,物体颜色改变

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

public class TransformModel : MonoBehaviour
{
    //鼠标经过时改变物体颜色
    private Color mouseOverColor = Color.blue;//声明变量为蓝色
    private Color originalColor;//声明变量来存储本来颜色

    void Start()
    {
        originalColor = GetComponent<Renderer>().sharedMaterial.color;//开始时得到物体着色
    }

    void onm ouseEnter()
    {
        GetComponent<Renderer>().material.color = mouseOverColor;//当鼠标滑过时改变物体颜色为蓝色
    }

    void onm ouseExit()
    {
        GetComponent<Renderer>().material.color = originalColor;//当鼠标滑出时恢复物体本来颜色
    }

    IEnumerator onm ouseDown()
    {
        Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三维物体坐标转屏幕坐标
        //将鼠标屏幕坐标转为三维坐标,再计算物体位置与鼠标之间的距离
        var offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        
        while (Input.GetMouseButton(0))
        {
            //将鼠标位置二维坐标转为三维坐标
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            //将鼠标转换的三维坐标再转换成世界坐标+物体与鼠标位置的偏移量
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
            transform.position = curPosition;
            yield return new WaitForFixedUpdate();//循环执行
        }
    }
}

  

二、鼠标控制3D物体旋转

1. 控制物体左右旋转,上下旋转

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

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

    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            //Input.GetAxis("MouseX")获取鼠标移动的X轴的距离
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            y = ClampAngle(y, yMinLimit, yMaxLimit);
            //欧拉角转化为四元数
            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. 控制摄像机以物体为中心旋转

脚本挂载到摄像机上

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();
    }
    //镜头的远离和接近
    public void Ctrl_Cam_Move()
    {
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            transform.Translate(Vector3.forward * 1f);//速度可调  自行调整
        }
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            transform.Translate(Vector3.forward * -1f);//速度可调  自行调整
        }
    }
    //摄像机的旋转
    public void Cam_Ctrl_Rotation()
    {
        var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动
        var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动
        if (Input.GetKey(KeyCode.Mouse1))
        {
            transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 5);
            transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5);
        }
    }
}

  

三、鼠标控制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. 基于摄像机的远近的缩放

脚本挂载到摄像机上

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

public class TransformModel : MonoBehaviour
{
    void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //鼠标滚动滑轮 值就会变化
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                //范围值限定
                if (Camera.main.fieldOfView <= 100)//摄像机采用Perspective透视
                    Camera.main.fieldOfView += 2;
                if (Camera.main.orthographicSize <= 20)//摄像机采用Orthograpic正交
                    Camera.main.orthographicSize += 0.5F;
            }
            //Zoom in  
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                //范围值限定
                if (Camera.main.fieldOfView > 2)//摄像机采用Perspective透视
                    Camera.main.fieldOfView -= 2;
                if (Camera.main.orthographicSize >= 1)//摄像机采用Orthograpic正交
                    Camera.main.orthographicSize -= 0.5F;
            }
        }
    }
}

  

 

标签:鼠标,Vector3,transform,旋转,Unity,using,Input,public,3D
From: https://www.cnblogs.com/clf125800/p/17215486.html

相关文章