当前位置:网站首页>Unity can realize the rotation, translation and scaling script of the camera around the target point on the mobile terminal device
Unity can realize the rotation, translation and scaling script of the camera around the target point on the mobile terminal device
2022-06-12 06:05:00 【VR technology Xiaoguang】
Share a Unity The camera can rotate around the target point on the mobile terminal device , translation , Scaled script , Available after testing .
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("Camera-Control/MobilemaxCamera")]
public class MobilemaxCamera : MonoBehaviour
{
public Transform target;
public Vector3 targetOffset;
public float distance = 5.0f;
public float maxDistance = 20;
public float minDistance = .6f;
public float xSpeed = 5.0f;
public float ySpeed = 5.0f;
public int yMinLimit = -80;
public int yMaxLimit = 80;
public float zoomRate = 10.0f;
public float panSpeed = 0.3f;
public float zoomDampening = 5.0f;
private float xDeg = 0.0f;
private float yDeg = 0.0f;
private float currentDistance;
private float desiredDistance;
private Quaternion currentRotation;
private Quaternion desiredRotation;
private Quaternion rotation;
private Vector3 position;
private Vector3 FirstPosition;
private Vector3 SecondPosition;
private Vector3 delta;
private Vector3 lastOffset;
private Vector3 lastOffsettemp;
//private Vector3 CameraPosition;
//private Vector3 Targetposition;
//private Vector3 MoveDistance;
void Start() { Init(); }
void OnEnable() { Init(); }
public void Init()
{
//If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
if (!target)
{
GameObject go = new GameObject("Cam Target");
go.transform.position = transform.position + (transform.forward * distance);
target = go.transform;
}
distance = Vector3.Distance(transform.position, target.position);
currentDistance = distance;
desiredDistance = distance;
//be sure to grab the current rotations as starting points.
position = transform.position;
rotation = transform.rotation;
currentRotation = transform.rotation;
desiredRotation = transform.rotation;
xDeg = Vector3.Angle(Vector3.right, transform.right);
yDeg = Vector3.Angle(Vector3.up, transform.up);
}
/*
* Camera logic on LateUpdate to only update after all character movement logic has been handled.
*/
void LateUpdate()
{
// If Control and Alt and Middle button? ZOOM!
if (Input.touchCount == 2)
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPreviousPosition = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePreviousPosition = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPreviousPosition - touchOnePreviousPosition).magnitude;
float TouchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagDiff = prevTouchDeltaMag - TouchDeltaMag;
desiredDistance += deltaMagDiff * Time.deltaTime * zoomRate * 0.0025f * Mathf.Abs(desiredDistance);
}
// If middle mouse and left alt are selected? ORBIT
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchposition = Input.GetTouch(0).deltaPosition;
xDeg += touchposition.x * xSpeed * 0.002f;
yDeg -= touchposition.y * ySpeed * 0.002f;
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
}
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
transform.rotation = rotation;
if (Input.GetMouseButtonDown(1))
{
FirstPosition = Input.mousePosition;
lastOffset = targetOffset;
}
if (Input.GetMouseButton(1))
{
SecondPosition = Input.mousePosition;
delta = SecondPosition - FirstPosition;
targetOffset = lastOffset + transform.right * delta.x * 0.003f + transform.up * delta.y * 0.003f;
}
Orbit Position
// affect the desired Zoom distance if we roll the scrollwheel
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
position = target.position - (rotation * Vector3.forward * currentDistance);
position = position - targetOffset;
transform.position = position;
}
private 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);
}
}
边栏推荐
- Houdini terrain creation
- Leetcode 第 80 場雙周賽題解
- Multiple ways 99.9% to solve the problem of garbled code after copying text from PDF
- JS变量作用域
- Unity vscode cannot jump to definition
- China's elastic belt market trend report, technical dynamic innovation and market forecast
- Brief summary of software project architecture
- EBook editing and deleting
- Unable to access this account. You may need to update your password or grant the account permission to synchronize to this device. Tencent corporate email
- dlib 人脸检测
猜你喜欢

A preliminary understanding of function

Leetcode-1535. Find the winner of the array game

Directx11 advanced tutorial tiled based deffered shading

Project and build Publishing

Directx11 advanced tutorial PBR (1) summary of physical phenomena of light

How do I get the date and time from the Internet- How to get DateTime from the internet?

(UE4 4.27) UE4 adds a customized meshpass to realize the edge illumination of the mobile terminal

First note

Houdini terrain creation

A month's worth of DDD will help you master it
随机推荐
IBL of directx11 advanced tutorial PBR (3)
How to split a row of data into multiple rows in Informix database
Introduction to thread pool: ThreadPoolExecutor
Json-c common APIs
Project and build Publishing
Database Experiment 2: data update
Why is the union index the leftmost matching principle?
Three years of sharpening a sword: insight into the R & D efficiency of ant financial services
China's elastic belt market trend report, technical dynamic innovation and market forecast
Review notes of naturallanguageprocessing based on deep learning
Leetcode-717. 1-bit and 2-bit characters (O (1) solution)
How do I get the date and time from the Internet- How to get DateTime from the internet?
Cross compile libev
Jpg format and XML format files are separated into different folders
Directx11 advanced tutorial PBR (1) summary of physical phenomena of light
Guns framework multi data source configuration without modifying the configuration file
数据库实验三:数据查询
Research Report on truffle fungus industry - market status analysis and development prospect forecast
项目管理与统筹
为什么数据库不使用二叉树、红黑树、B树、Hash表? 而是使用了B+树