鼠标左键控制相机的旋转

鼠标左键控制场景视图
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上面。在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

unity学习社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值