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物体。以下是一些常用的方法: 1. 摄像机控制:在Unity中,可以通过摄像机控制来实现自由查看3D物体。可以使用鼠标控制摄像机的旋转、缩放和移动,以便在场景中查看物体的不同角度和位置。可以使用Unity提供的摄像机脚本或自定义脚本来实现这些功能。 2. 物体交互:可以通过在物体上添加交互组件,让用户与物体进行交互,从而实现自由查看。例如,可以给物体添加拖拽组件,让用户通过鼠标拖拽物体来移动和旋转;也可以添加点击组件,在物体上点击时切换不同的视角。这些交互组件可以通过Unity的脚本系统编写或使用现成的插件。 3. 视角切换:在Unity中,可以通过切换不同的摄像机或相机视角来实现自由查看3D物体。可以创建多个摄像机,分别定位到不同的角度和位置,并在需要时切换摄像机来实现不同视角的查看。 4. 虚拟现实 (VR) 和增强现实 (AR) 技术:Unity提供了强大的虚拟现实和增强现实技术,可以让用户通过VR头盔或AR设备与3D物体进行互动。用户可以自由移动头盔或设备来查看物体的不同角度和位置,增强沉浸感和交互性。 总之,Unity提供了丰富的功能和工具来实现自由查看3D物体。可以根据项目需求和个人技术水平选择适合的方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值