首 页 行业热点 新车 试驾评测 养车用车 车型库
当前位置:首页Unity3D鼠标操作实现旋转(相机围绕目标物体)、缩放观察物体

Unity3D鼠标操作实现旋转(相机围绕目标物体)、缩放观察物体

2020-12-02 来源:好土汽车网
导读 Unity3D鼠标操作实现旋转(相机围绕目标物体)、缩放观察物体
Unity3D⿏标操作实现旋转(相机围绕⽬标物体)、缩放观察物

//放在摄像头上,实现旋转、缩放观察物体using UnityEngine;

public class BaseCam : MonoBehaviour{

  ///缩放距离

  public float ZoomSpeed=30;  //拖动速度

  public float MovingSpeed=0.5f;  //视⾓和摆动速度

  public float RotateSpeed=1;  //摄像机旋转速度  public float distance=30;

  public Transform rotateCenter;

public Vector3 mousePos1; //记录⿏标点下去瞬间的位置 public Vector3 mousePos2; //记录⿏标任何时刻的位置 public Quaternion start_qua; //⾓度使⽤四元数 public Vector3 start_pos; //位置坐标 // Use this for initialization void Start() {

//记录相机开始的⾓度与位置 start_qua = transform.rotation; start_pos = transform.position; }

  Quaternion rotation;  Vector3 position;

  float delta_x,delta_y,delta_z;

  float delta_rotation_x,delta_rotation_y;

// Update is called once per frame void Update() {

    //拖动

    if(Input.GetMouseButton(2))    {

      delta_x=Input.GetAxis(\"Mouse X\")*MovingSpeed;      delta_y=Input.GetAxis(\"Mouse Y\")*MovingSpeed;

      rotation=Quaternion.Euler(0,transform.rotation.eulerAngles.y,0);

      transform.position=rotation*new Vector3(-delta_x,-delta_y,0)+transform.position;    }

    //缩放

    if(Input.GetAxis(\"Mouse ScrollWheel\")!=0)    {

      delta_z=-Input.GetAxis(\"Mouse ScrollWheel\")*ZoomSpeed;      transform.Translate(0,0,-delta_z);      //distance+=delta_z;    }

//记录⿏标实时移动的点

mousePos2 = Input.mousePosition;

    //旋转中⼼为⾃⾝(相机)    if(Input.GetMouseButton(1))    {

      delta_rotation_x=Input.GetAxis(\"Mouse X\")*RotateSpeed;      delta_rotation_y=-Input.GetAxis(\"Mouse Y\")*RotateSpeed;

      position=transform.rotation*new Vector3(0,0,distance)+transform.position;

      transform.Rotate(0,delta_rotation_x,0,Space.World);      transform.Rotate(delta_rotation_y,0,0);

      transform.position=transform.rotation*new Vector3(0,0,-distance)+position;    }   }

void OnGUI() {

//⿏标左键

if (Input.GetMouseButton(0)) {

mousePos1 = Input.mousePosition; //记录⿏标点击瞬间的点 Vector3 offset = mousePos1 - mousePos2; //记录⿏标移动的距离

//上下与左右 旋转分开,绝对值⽐较

if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)) {

//以物体上⽅为旋转轴(Vector3.up == new Vector3(0, 1.0f, 0)),物体左右旋转⾓度与⿏标横向移动距离相关,变化速率2f transform.RotateAround(new Vector3(0, 0, 0), Vector3.up, Time.deltaTime * offset.x * 2f);

         // transform.RotateAround(rotateCenter.localPosition, Vector3.up, Time.deltaTime * offset.x * 2f); //旋转中⼼为⽬标物体rotateCenter } else {

//以世界坐标右⽅为旋转轴(transform.right,是会变化的量),物体上下旋转⾓度与⿏标纵向移动距离相关,变化速率2f transform.RotateAround(new Vector3(0, 0, 0), transform.right, -Time.deltaTime * offset.y * 2f);//旋转中⼼为世界

// transform.RotateAround(rotateCenter.localPosition, transform.right, -Time.deltaTime * offset.y * 2f);//旋转中⼼为⽬标物体rotateCenter

}

//打印数据transform.right变量

Debug.Log(\"pos: \" + transform.right); }

////⿏标中键,物体恢复原来的⾓度与位置 // if (Input.GetMouseButton(2)) // {

// transform.rotation = start_qua; // transform.position = start_pos; // }

////⿏标中键滑动,物体缩放,摄像头前后移动距离范围在2f~5f,变化速率3f

// if (Input.GetAxis(\"Mouse ScrollWheel\") > 0f && Vector3.Distance(transform.position, new Vector3(0, 0, 0)) > 2f) // {

// transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis(\"Mouse ScrollWheel\") * 5f);

// }

// if (Input.GetAxis(\"Mouse ScrollWheel\") < 0f && Vector3.Distance(transform.position, new Vector3(0, 0, 0)) <5f) // {

// transform.Translate(Vector3.back * Time.deltaTime * (-Input.GetAxis(\"Mouse ScrollWheel\")) * 5f); //}// }}

因篇幅问题不能全部显示,请点此查看更多更全内容