一、鼠標(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ī)上文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-513117.html
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)!