您当前的位置: 首页 > 

苍狼王unity学院

暂无认证

  • 0浏览

    0关注

    305博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

鼠标左键控制相机的旋转

苍狼王unity学院 发布时间:2019-03-11 17:32:51 ,浏览量:0

鼠标左键控制场景视图 1、 挂载:脚本挂载到主摄像机上 using UnityEngine; using System.Collections; /// /// 挂载:脚本挂载到主摄像机上 /// 功能:通过鼠标控制相机旋转、拖动、拉伸 /// public class CameraMove : MonoBehaviour { //目标物体、高度、距离 public Transform target; public float targetHeight = 1.7f; public float distance = 5.0f;

//滚轮的速度和最大、最小距离
public int zoomRate = 40;
public float maxDistance = 20;
public float minDistance = .6f;

//X和Y轴的旋转速度
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;

//限制Y轴的最大、最小角度
public int yMinLimit = 0;
public int yMaxLimit = 80;

[SerializeField]
private  float x = 0.0f;

[SerializeField]
private  float y = 0.0f;

[SerializeField]
private float correctedDistance;

[SerializeField]


 void Start ()
{
    Vector3 angles = transform.eulerAngles;
    y = angles.x;
    x = angles.y;
}
void LateUpdate ()
{
    // 如果目标没有定义,就不要做任何事情
    Move();
}

public void Move()
{
	if (Input.GetMouseButton(0))
	{
		x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
		y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
	}
	
    //限制Y轴范围
    y = ClampAngle(y, yMinLimit, yMaxLimit);

    //设置相机的旋转
    Quaternion rotation = Quaternion.Euler(y, x, 0);
    transform.rotation = rotation;
    
}

private 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);

}

}

2、新建Cube,拖放到Target上面。在这里插入图片描述

关注
打赏
1665389160
查看更多评论
立即登录/注册

微信扫码登录

0.0352s