1. 程式人生 > >Unity滑鼠自由檢視3D物體之控制攝相機Camera

Unity滑鼠自由檢視3D物體之控制攝相機Camera

這種方式是通過控制相機的旋轉,位置來檢視物體。

下面的程式碼是通過修改官方的MouseOrbit程式碼產生,加了縮放功能,以及縮放,旋轉,位移的緩動,讓它不再那麼生硬。

需要將下面的程式碼放到相機上面。

using UnityEngine;
using System.Collections;

public class MouseOrbitImproved : MonoBehaviour {
	
	public Transform target;
	public float distance = 8.0f;
	public float xSpeed = 70.0f;
	public float ySpeed = 50.0f;
	
	public float yMinLimit = 0f;
	public float yMaxLimit = 90f;
	
	public float distanceMin = 8f;
	public float distanceMax = 15f;
	public float zoomSpeed=0.5f;
	
	private Rigidbody rigidbody;
	
	private float x = 0.0f;
	private float y = 0.0f;

	private float fx=0f;
	private float fy=0f;
	private float fDistance=0;
	
	// Use this for initialization
	void Start () 
	{
		Vector3 angles = transform.eulerAngles;
		x = angles.y;
		y = angles.x;
		fx = x;
		fy = y;
		
		rigidbody = GetComponent<Rigidbody>();
		
		// Make the rigid body not change rotation
		if (rigidbody != null)
		{
			rigidbody.freezeRotation = true;
		}
		UpdateRotaAndPos ();
		fDistance = distance;
	}

	void Update()
	{
		// If there are two touches on the device...
		if (Input.touchCount == 2) {
			// Store both touches.
			Touch touchZero = Input.GetTouch (0);
			Touch touchOne = Input.GetTouch (1);
			
			// Find the position in the previous frame of each touch.
			Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
			Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
			
			// Find the magnitude of the vector (the distance) between the touches in each frame.
			float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
			float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
			
			// Find the difference in the distances between each frame.
			float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
			fDistance = Mathf.Clamp ( distance+ deltaMagnitudeDiff * zoomSpeed , distanceMin, distanceMax);
		}
		distance = Mathf.Lerp (distance, fDistance, 0.25f);
	}

	void LateUpdate () 
	{
		if (Input.GetMouseButton(0)&&Input.touchCount<2)
		{
			if (target) {
				float dx = Input.GetAxis("Mouse X");
				float dy = Input.GetAxis("Mouse Y");
				if (Input.touchCount > 0)
				{
					dx = Input.touches[0].deltaPosition.x;
					dy = Input.touches[0].deltaPosition.y;
				}
				
				x += dx * xSpeed  * Time.deltaTime;//*distance
				y -= dy * ySpeed * Time.deltaTime;
				
				y = ClampAngle(y, yMinLimit, yMaxLimit);
			}
		}

		fx = Mathf.Lerp (fx, x, 0.2f);
		fy = Mathf.Lerp (fy, y, 0.2f);

		UpdateRotaAndPos ();
	}


	void UpdateRotaAndPos(){
		if (target) {
			Quaternion rotation = Quaternion.Euler(fy, fx, 0);
			Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
			Vector3 position = rotation * negDistance + target.position;
			
			transform.rotation = rotation;
			transform.position = position;
		}
	}
	
	public static float ClampAngle(float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp(angle, min, max);
	}
}


相關推薦

Unity滑鼠自由檢視3D物體控制相機Camera

這種方式是通過控制相機的旋轉,位置來檢視物體。 下面的程式碼是通過修改官方的MouseOrbit程式碼產生,加了縮放功能,以及縮放,旋轉,位移的緩動,讓它不再那麼生硬。 需要將下面的程式碼放到相機上面。 using UnityEngine; using System.Col

unity按下滑鼠拖動3D物體指令碼

public class ObjectDragger : MonoBehaviour { private Vector3 screenPoint; private Vector3 offset; bool dragged = false; private V

unity--滑鼠放上顯示物體資訊和物體高光效果

滑鼠放在物體上顯示資訊程式碼 using UnityEngine; using System.Collections; public class info : MonoBehaviour { bool isShowInfo; public GUISty

Unity 通過UGUI實現3D物體點選事件

1.給主攝像機新增Physics Raycaster指令碼;(檢測源)2.新增UGUI EventSystem (UGUI事件系統)3.寫指令碼,實現IPointerClickHandler介面,將指令碼掛載到被點選物體上;using System.Collections;

unity畫線對映到3D物體

1.畫線方式有很多 包括OpenGL,shader ,linerender,也可以直接動態更改貼圖的畫素點顏色 這裡是用的GL,如果不是太懂gl的可以參考unity 官方API,其實我也不是太懂~~~~ using UnityEngine; using System.Collection

Unity在UI介面上顯示3D模型/物體控制模型旋轉

Unity3D物體在UI介面的顯示 本文提供全流程,中文翻譯。 Chinar 堅持將簡單的生活方式,帶給世人!(擁有更好的閱讀體驗 —— 高解析度使用者請根據需求調整網頁縮放比例) Chinar —— 心分享、心創新!助力快速利用 UGUI

Unity基礎篇:Unity使用滑鼠拖動2D和3D物體

 /*     首先說一下,Input.mousePosition是滑鼠所在畫素平面內的座標,需要根據自己的需求轉變成世界座標。         Description 描述         The

unity 滑鼠控制攝像機圍繞物體旋轉

void LateUpdate() //對攝像機的操作寫在LateUpdate裡 { x += Input.GetAxis("Mouse X") * rotateSpeed

[Unity]2D&3D物體指向indicator滑鼠,技能指示器 基礎

--------------------------------------------------------------------------------------------------------------------------------------

關於Unity視頻播放器插件 AVPro Video(二)3D物體上視頻播放

org 圖片 平面 type http com 項目創建 attr data 1.官網下載該插件或者我分享的鏈接: 鏈接:https://pan.baidu.com/s/1boGeJ8r 密碼:mvbf 2.新建項目創建主要控制物體 3.創建對於視頻播放在3D物體身上的

unity 滑鼠懸浮於物體物體變色

void OnMouseOver()//滑鼠懸浮於按鈕之上,按鈕顏色變化 { this.transform.Find("default").GetComponent<MeshRenderer>().material.color = new Colo

unity3d嵌入到iOS工程(3d物體和iOS控制元件顯示在同一個view上)

最近公司要開發一款用於3d展示的應用,通過在網上拜讀各大神的帖子,終於完美解決此類問題。(unity3d版本2017.3,xcode9.2) 前邊unity3d打包iOS工程的方法在此就不做贅述,其他帖子基本可以解決,本帖直接上乾貨。 1、在打包好的iOS工程中,找到UnityAppCont

Unity遊戲開發控制相機跟隨主角

using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowTarget : MonoBehaviour {          priva

Unity滑鼠控制相機上下左右環視360度旋轉(Quaternion.AngleAxis)

之前實現的是相機的360度旋轉,用的是LocalEulerAngle,這一篇文章實現用的是Quaternion.AngleAxis,這個方法將繞某個軸旋轉的角度轉為四元數 目前的四元數=初始的四元數*繞X軸轉的四元數*繞Y軸轉的四元數  理解也比較好理解,就是繞某個軸旋轉

unity 滑鼠拖拽物體移動

    private Camera cam;//發射射線的攝像機     private GameObject go;//射線碰撞的物體     private Vector3 screenSpace;     pri

詳解·高通Vuforia識別追蹤3D物體/模型,Unity開發

研究這個問題的初衷: 1.公司要做一個識別3D物體的案例,參考了各個平臺,發現EasyAR和Vuforia支援3D物體識別與追蹤。 2.剛開始用EasyAR做,但做了一半發現有一定的限制: 1).必須Pro版本的EA外掛才能支援3D物體識別與追蹤; 2).Pro版本雖然可以免費試用,但是每天

Unity實用小工具或指令碼——3D物體帶座標軸的拖拽

一、前言      我們最近要做一個線路的規劃編輯,並且是在三維場景中,編輯完就立馬能用。立馬能用還好說,有特別多的輪子可以用,在三維場景中實時編輯就有點意思了。其實功能就是類似於在Unity的編輯介面操作一個Cube的位置,當然旋轉什麼的我這個任務裡暫時還不需要,就先簡單

面向元件程式設計Unity 6.怎樣複製遊戲物體 關鍵字:time//Instantiate/Debug

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Transfo

unity如何實現3D物體疊加到攝像頭畫面上

實現效果如下: 功能: 1 開啟攝像頭,攝像頭畫面作為軟體的背景 2 3d物體放在攝像頭畫面之上 具體實現如下: 具體程式碼實現如下: using System.Collections; using Sy

unity 滑鼠控制第一人稱視角及鍵盤控制移動

指令碼MouseLook(在主相機上): using UnityEngine; using System.Collections; public class Mouselook : MonoBehaviour { public enum RotationAxes{