当前位置:网站首页>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;
}
}
边栏推荐
- Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
- Draw on screen border in Commodore 64
- [vcs+verdi joint simulation] ~ take the counter as an example
- How can the international trading platform for frying US crude oil guarantee capital security?
- What is multimodal interaction?
- Writing unityshader with sublimetext
- C # three ways to obtain web page content
- Unity realizes rotation and Revolution
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- svg和canvas的区别
猜你喜欢

力扣977. 有序数组的平方

Unity/ue reads OPC UA and OPC Da data (UE4)

Unity3d packaging and publishing APK process

Unity script life cycle and execution sequence

Create a simple battle game with photon pun

Force buckle 977 Square of ordered array

SCM learning notes: interrupt learning

Unity project hosting platform plasticscm (learn to use 2)

Database base (Study & review for self use)

redis集群概念
随机推荐
A collection of errors encountered in machine learning with unity
Redis cluster concept
Have a heart beating Valentine's day in Singapore
Force buckle 59 Spiral matrix II
HTC vive cosmos development - handle button event
0 foundation starts self-study unit notes control direction becomes larger
Unit screenshot saved on the phone
Draw on screen border in Commodore 64
redis集群概念
Force buckle 209 Minimum length subarray
Unreal 4 learning notes - set player birth point
炒美原油的国际交易平台如何能保障资金安全呢?
Chapter 8 primitive processing of OpenGL super classic (version 7)
Pit of smoothstep node in shadergraph
How can the international trading platform for frying US crude oil guarantee capital security?
Special folders in unity3d and their meanings
Nestjs中控制器和路由的配置使用
C # Foundation
Unity/ue reads OPC UA and OPC Da data (UE4)
Unity realizes rotation and Revolution