小弟苦逼学生一枚,老师布置了任务,做不下去了,。。。
要求是:
鼠标控制摄像机自由浏览模型。拖住左键自由旋转视角,拖住右键移动摄像机画面,中键控制视距,下面是我改的脚本。
希望改进成:拖住右键移动摄像机画面后,再拖住左键旋转视角,旋转中心依然在屏幕中心,而不是模型的中心。我的QQ是297388918,小弟初学者,要是哪位大神有空指点下就好了,可以小偿
using UnityEngine;
using System.Collections;
public class Guan9 : MonoBehaviour
{
//摄像机参照的模型
public Transform target;
//旋转中心参照物
//public Transform Cz;
//摄像机距离默认的距离
public float normalDistance=200.0f;
//鼠标X轴、Y轴移动的角度
float x;
float y;
//鼠标X轴,y轴移动的距离
float xd;
float yd;
private Vector3 pd;
//限制旋转角度的最小值与最大值,yMinLimint是从底部看,yMaxLimit是从上方看
float yMinLimit = 0.0f;
float yMaxLimit = 90.0f;
//X、Y轴移动速度
float xSpeed = 250.0f;
float ySpeed = 120.0f;
public float MouseWheelSensitivity=100.0f;
public float MouseZoomMin=200.0f;
public float MouseZoomMax=800.0f;
void Start ()
{
//初始化X、Y轴角度等于参照模型的角度
Vector2 angles = transform.eulerAngles;
//模型的位置存储在旋转中心参照物上
//Cz.position = target.position;
x = angles.y;
y = angles.x;
//鼠标右键移动视窗的初始变量值
xd = 0.0f;
yd = 0.0f;
if (rigidbody)
rigidbody.freezeRotation = true;
}
void LateUpdate ()
{
if (target)
{
//中键控制视距
if (Input.GetAxis ("Mouse ScrollWheel") != 0)
{
//Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
//Debug.Log(normalDistance);
if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax) {
normalDistance -= Input.GetAxis ("Mouse ScrollWheel") * MouseWheelSensitivity;
}
if (normalDistance < MouseZoomMin) {
normalDistance = MouseZoomMin;
}
if (normalDistance > MouseZoomMax)
{
normalDistance = MouseZoomMax;}
}
//自由浏览模型改变视角
if (Input.GetMouseButton (0))
{
//根据鼠标移动修改相机的角度
x += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle (y, yMinLimit, yMaxLimit);
}
//右键拖动相机窗口
if (Input.GetMouseButton (1))
{
//根据鼠标移动修改相机的位置
xd += Input.GetAxis ("Mouse X") * xSpeed * 0.01f;
yd += Input.GetAxis ("Mouse Y") * ySpeed * 0.01f;
pd =new Vector3(xd, yd, 0.0f);
}
Quaternion rotation = Quaternion.Euler (y, x, 0);
Vector3 position = rotation * new Vector3 (0.0f, 0.0f, -normalDistance) + target.position;
//设置模型的位置与旋转
transform.rotation = rotation;
transform.position = position;
//右键移动相机窗口的距离
transform.Translate(pd,Space.Self);
//transform.LookAt(target.transform.Translate(pd,Camera.main.transform));
}
}
float ClampAngle (float angle , float min , float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
}
要求是:
鼠标控制摄像机自由浏览模型。拖住左键自由旋转视角,拖住右键移动摄像机画面,中键控制视距,下面是我改的脚本。
希望改进成:拖住右键移动摄像机画面后,再拖住左键旋转视角,旋转中心依然在屏幕中心,而不是模型的中心。我的QQ是297388918,小弟初学者,要是哪位大神有空指点下就好了,可以小偿

using UnityEngine;
using System.Collections;
public class Guan9 : MonoBehaviour
{
//摄像机参照的模型
public Transform target;
//旋转中心参照物
//public Transform Cz;
//摄像机距离默认的距离
public float normalDistance=200.0f;
//鼠标X轴、Y轴移动的角度
float x;
float y;
//鼠标X轴,y轴移动的距离
float xd;
float yd;
private Vector3 pd;
//限制旋转角度的最小值与最大值,yMinLimint是从底部看,yMaxLimit是从上方看
float yMinLimit = 0.0f;
float yMaxLimit = 90.0f;
//X、Y轴移动速度
float xSpeed = 250.0f;
float ySpeed = 120.0f;
public float MouseWheelSensitivity=100.0f;
public float MouseZoomMin=200.0f;
public float MouseZoomMax=800.0f;
void Start ()
{
//初始化X、Y轴角度等于参照模型的角度
Vector2 angles = transform.eulerAngles;
//模型的位置存储在旋转中心参照物上
//Cz.position = target.position;
x = angles.y;
y = angles.x;
//鼠标右键移动视窗的初始变量值
xd = 0.0f;
yd = 0.0f;
if (rigidbody)
rigidbody.freezeRotation = true;
}
void LateUpdate ()
{
if (target)
{
//中键控制视距
if (Input.GetAxis ("Mouse ScrollWheel") != 0)
{
//Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
//Debug.Log(normalDistance);
if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax) {
normalDistance -= Input.GetAxis ("Mouse ScrollWheel") * MouseWheelSensitivity;
}
if (normalDistance < MouseZoomMin) {
normalDistance = MouseZoomMin;
}
if (normalDistance > MouseZoomMax)
{
normalDistance = MouseZoomMax;}
}
//自由浏览模型改变视角
if (Input.GetMouseButton (0))
{
//根据鼠标移动修改相机的角度
x += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle (y, yMinLimit, yMaxLimit);
}
//右键拖动相机窗口
if (Input.GetMouseButton (1))
{
//根据鼠标移动修改相机的位置
xd += Input.GetAxis ("Mouse X") * xSpeed * 0.01f;
yd += Input.GetAxis ("Mouse Y") * ySpeed * 0.01f;
pd =new Vector3(xd, yd, 0.0f);
}
Quaternion rotation = Quaternion.Euler (y, x, 0);
Vector3 position = rotation * new Vector3 (0.0f, 0.0f, -normalDistance) + target.position;
//设置模型的位置与旋转
transform.rotation = rotation;
transform.position = position;
//右键移动相机窗口的距离
transform.Translate(pd,Space.Self);
//transform.LookAt(target.transform.Translate(pd,Camera.main.transform));
}
}
float ClampAngle (float angle , float min , float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
}