当前位置:网站首页>Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
2022-06-30 05:07:00 【zjh_ three hundred and sixty-eight】
This script hangs on Camera( The camera ) On .
Locating a model is used to locate the model in front of it after finding it , Then it has the following operation functions ;
Similar to the operation in editor mode , Use as browsing model , rotate 、 Dragging and scaling are both operational Camera( The camera ) Displacement ;
Highlight used HighlightingSystem plug-in unit ;
The center point is not of the model transform.position, It's the center of the model , Because some models position Not the center of the model , So this method is needed .
using HighlightingSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test01 : MonoBehaviour {
public Transform obj;
float maxX, maxY, maxNum;
//float minx, minY, minZ;
private Vector3 centerPoint;
#region Browse model manipulation properties
/// <summary>
/// Zoom distance
/// </summary>
public float ZoomSpeed = 30;
/// <summary>
/// Drag speed
/// </summary>
public float MovingSpeed = 0.5f;
/// <summary>
/// Angle of view swing speed
/// </summary>
public float RotateSpeed = 1;
/// <summary>
/// Camera rotation speed
/// </summary>
public float distance = 30;
Quaternion rotation;
Vector3 position;
float delta_x, delta_y, delta_z;
float delta_rotation_x, delta_rotation_y;
#endregion
void Start () {
// Calculate the position point directly in front of the model
Mesh mesh = obj.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
//minZ = obj.position.z;
// Calculate the maximum vertex , Judge the longest or widest of the model , Used to calculate the distance from the positioning point to the model
foreach (Vector3 item in vertices)
{
if (item.x > maxX)
{
maxX = item.x;
}
if (item.y > maxY)
{
maxY = item.y;
}
}
centerPoint = GetCenter(obj.gameObject);
// Get the longest end of the model as the calculated viewpoint distance
if (maxX > maxY)
{
maxNum = maxX;
}
else
{
maxNum = maxY;
}
if (maxNum < 5) maxNum = 5;
// Obtain the viewpoint directly in front of the model
Camera.main.transform.position = new Vector3(centerPoint.x, centerPoint.y, centerPoint.z - maxNum);
//================================================
// Method 2 : If obj The object is static , In this way
//Renderer[] mrs = obj.GetComponentsInChildren<Renderer>();
//Vector3 center = obj.transform.position;
//if (mrs.Length != 0)
//{
// Bounds bounds = new Bounds(center, Vector3.zero);
// foreach (Renderer item in mrs)
// {
// bounds.Encapsulate(item.bounds);
// }
// center = bounds.center;
// print(" Center point :" + center + " The biggest point :" + bounds.max + " The smallest :" + bounds.min);
// if ((bounds.max.x - bounds.min.x) > (bounds.max.y - bounds.min.y))
// {
// maxNum = bounds.max.x - bounds.min.x;
// }else if((bounds.max.x - bounds.min.x) < (bounds.max.y - bounds.min.y))
// {
// maxNum = bounds.max.y - bounds.min.y;
// }
// else
// {
// maxNum = bounds.max.x - bounds.min.x;
// }
// print(maxNum);
// // The limit model is too small , Cause positioning too close
// if (maxNum < 8)
// {
// maxNum = 8;
// }
// print(maxNum);
//}
//centerPoint = center;
//================================================
// The highlighted
Highlighter highlighter = obj.gameObject.AddComponent<Highlighter>();// Add highlight script
highlighter.ConstantOn();
}
// Update is called once per frame
void Update()
{
// Drag the
if (Input.GetMouseButton(2))
{
// Adjust the drag sensitivity in real time according to the distance
MovingSpeed = Vector3.Distance(transform.position, centerPoint) / 40;
delta_x = Input.GetAxis("Mouse X") * MovingSpeed;
delta_y = Input.GetAxis("Mouse Y") * MovingSpeed;
//rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
//transform.position = rotation * new Vector3(-delta_x, 0, -delta_y) + transform.position;
transform.Translate(-delta_x, -delta_y, 0);
}
// The zoom
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
// Zoom distance limit
if(Vector3.Distance(transform.position, centerPoint) < 5f)
{
delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
if (delta_z > 0)
{
transform.Translate(0, 0, -delta_z);
}
}else if (Vector3.Distance(transform.position, centerPoint) > maxNum*2f)
{
delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
if (delta_z < 0)
{
transform.Translate(0, 0, -delta_z);
}
}
else
{
delta_z = -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
transform.Translate(0, 0, -delta_z);
}
}
// rotate
if (Input.GetMouseButton(1))
{
// Adjust the rotation sensitivity according to the distance
//distance = Vector3.Distance(transform.position, centerPoint);
delta_rotation_x = Input.GetAxis("Mouse X") * RotateSpeed;
delta_rotation_y = -Input.GetAxis("Mouse Y") * RotateSpeed;
position = transform.rotation * new Vector3(0, 0, distance) + transform.position;
transform.Rotate(0, delta_rotation_x, 0, Space.World);
transform.Rotate(delta_rotation_y, 0, 0);
transform.position = transform.rotation * new Vector3(0, 0, -distance) + position;
// Rotate the target object
//delta_rotation_x = Input.GetAxis("Mouse X") * RotateSpeed;
//delta_rotation_y = Input.GetAxis("Mouse Y") * RotateSpeed;
//transform.Rotate(0, delta_rotation_x, 0);
//transform.Rotate(delta_rotation_y, 0, 0, Space.World);
}
}
/// <summary>
/// Get the center point
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private Vector3 GetCenter(GameObject target)
{
Renderer[] mrs = target.GetComponentsInChildren<Renderer>();
Vector3 center = target.transform.position;
if (mrs.Length != 0)
{
Bounds bounds = new Bounds(center, Vector3.zero);
foreach (Renderer item in mrs)
{
bounds.Encapsulate(item.bounds);
}
center = bounds.center;
}
return center;
}
}
边栏推荐
- Chapter 11 advanced data management of OpenGL super classic (version 7)
- Using the command line to convert JSON to dart file in fluent
- 力扣589:N 叉树的前序遍历
- Malignant bug: 1252 of unit MySQL export
- 003-JS-DOM-Attr-innerText
- Unreal 4 learning notes - set player birth point
- Procedural animation -- inverse kinematics of tentacles
- Unreal 4 unavigationsystemv1 compilation error
- Solution to 293 problems in the week of Li Kou
- Unit screenshot saved on the phone
猜你喜欢

Unity3d lookat parameter description

Force buckle 349 Intersection of two arrays

中文版PyCharm改为英文版PyCharm

ParticleSystem in the official Manual of unity_ Collision module

力扣27. 移除元素

Exploration of unity webgl

Unity is associated with vs. there is a compiler problem when opening

Some books you should not miss when you are new to the workplace

Li Kou 2049: count the number of nodes with the highest score

Basic operations of Oracle data
随机推荐
003-JS-DOM-Attr-innerText
GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
Li Kou 2049: count the number of nodes with the highest score
Unity3d learning notes-1 (C # learning)
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
JPA复合主键使用
Generate a slice of mesh Foundation
MinGW-w64下载文件失败the file has been downloaded incorrectly!
Unity dotween plug-in description
The difference between SVG and canvas
Force buckle 977 Square of ordered array
PWN入门(2)栈溢出基础
Solution to the 292 week match of Li Kou
Read and save txt files
Basic operations of Oracle data
SCM learning notes: interrupt learning
很紧张,第一天做软件测试,需要做什么?
Sourcetree usage
Yolov5 torch installation
Nestjs中控制器和路由的配置使用