unity 点击鼠标左键控制物体360展示
2015-02-11 15:01
369 查看
//把代码拖到摄像机上,target附在所要展示的物体上
public class MouseFollowRotation : MonoBehaviour {
public Transform target;
public float xSpeed=200, ySpeed=200, mSpeed=10;
public float yMinLimit=-50, yMaxLimit=50;
public float distance=7, minDistance=2, maxDistance=30;
//bool needDamping = false;
public bool needDamping =true;
float damping = 5.0f;
public float x = 0.0f;
public float y = 0.0f;
// Use this for initialization
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
// Update is called once per frame
void LateUpdate ()
{
if (target)
{
//use the light button of mouse to rotate the camera
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);
//print(Input.GetAxis("Mouse X"));
//print( Input.GetAxis("Mouse Y"));
//print(x);
//print(y);
}
distance -= Input.GetAxis("Mouse ScrollWheel")*mSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3( 0.0f, 0.0f, -distance );
Vector3 position = rotation * disVector + target.position;
//adjust the camera
if( needDamping )
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime*damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime*damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
}
}
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);
}
}
public class MouseFollowRotation : MonoBehaviour {
public Transform target;
public float xSpeed=200, ySpeed=200, mSpeed=10;
public float yMinLimit=-50, yMaxLimit=50;
public float distance=7, minDistance=2, maxDistance=30;
//bool needDamping = false;
public bool needDamping =true;
float damping = 5.0f;
public float x = 0.0f;
public float y = 0.0f;
// Use this for initialization
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
// Update is called once per frame
void LateUpdate ()
{
if (target)
{
//use the light button of mouse to rotate the camera
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);
//print(Input.GetAxis("Mouse X"));
//print( Input.GetAxis("Mouse Y"));
//print(x);
//print(y);
}
distance -= Input.GetAxis("Mouse ScrollWheel")*mSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3( 0.0f, 0.0f, -distance );
Vector3 position = rotation * disVector + target.position;
//adjust the camera
if( needDamping )
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime*damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime*damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
}
}
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);
}
}
相关文章推荐
- Unity鼠标控制物体360°旋转和点击移动
- Unity学习2 鼠标点击控制物体移动
- unity让物体移动到鼠标点击点
- unity 在鼠标点击位置生成物体
- Unity中鼠标点击地面控制人物移动
- Unity 鼠标点击屏幕创建物体
- 我的Unity(1)一点一滴 鼠标点击任意位置,物体移动到该位置。
- unity 用鼠标控制相机绕着物体旋转脚本
- unity 鼠标拖动 物体旋转 点击变色 拖动移动
- 【Unity快速实现小功能】快速添加一个鼠标点击物体的识别功能
- Unity获取鼠标点击位置,物体朝鼠标点击处移动
- Unity控制鼠标移动及点击
- 鼠标控制物体移动360旋转和视距
- Unity中获取鼠标位置,且点击鼠标右键则在点击位置显示物体
- unity实现鼠标点击后拖拽物体
- Unity简单利用屏幕坐标转换实现鼠标控制物体移动
- Unity之一天一个技术点(十四)---通过鼠标控制镜头绕着某物体旋转
- unity检测鼠标是点击在UI上还是物体上
- unity 鼠标控制摄像机围绕物体旋转
- Unity中物体如何移动到鼠标点击位置