unity 鼠标360度旋转摄像机,带角度限制

public Transform target;//相机的目标
    private float xSpeed = 200;//x轴的旋转速度
    private float ySpeed = 150;//x轴的旋转速度
    public float yMin = -20;//y最小角度
    public float yMax = 20;//y最大角度
    public bool needDamping = true; //是否需要的阻尼
    private float damping = 5;//阻尼 
    private float x = 0;
    private float y = 0;


    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }

    void LateUpdate()
    {
        if (target)
        {

            //使用鼠标光按钮来控制相机,调整照相机位置
            if (Input.GetMouseButton(0))
            {
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                y = ClampAngle(y, yMin, yMax);

            }

            Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
            Vector3 disVector = new Vector3(0.0f, 0.0f, 0);
            Vector3 position = rotation * disVector + target.position;

            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;
            }
        }
    }

    //限制旋转角度
    private float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }

    private void Update()
    {
        //视野放大缩小
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (GetComponent<Camera>().fieldOfView >= 30)
            {
                GetComponent<Camera>().fieldOfView--;
            }
        }

        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if (GetComponent<Camera>().fieldOfView <= 80)
            {
                GetComponent<Camera>().fieldOfView++;
            }

        }

    }

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Unity 是一款非常强大的游戏开发引擎,可以实现各种虚拟现实和仿真应用。要在 Unity 中实现物体360旋转缩放,可以通过以下步骤来完成。 首先,我们需要在 Unity 中创建一个新的场景,并在场景中添加一个需要旋转和缩放的物体。可以是一个 3D 模型或者一个简单的立方体。 接下来,在场景中创建一个空对象(Empty Object),将该空对象命名为 "RotationController"(旋转控制器)。 然后,将需要旋转和缩放的物体作为子对象添加到旋转控制器下面。 接着,为旋转控制器添加脚本,并在脚本中编写代码。 代码示例: using UnityEngine; public class RotationController : MonoBehaviour { public float rotationSpeed = 10f; public float scaleSpeed = 0.1f; void Update() { // 获取鼠标左右移动的距离并实现旋转 float rotationX = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime; transform.Rotate(0, rotationX, 0); // 获取鼠标滚轮的滚动距离并实现缩放 float scale = Input.GetAxis("Mouse ScrollWheel") * scaleSpeed; transform.localScale += new Vector3(scale, scale, scale); } } 通过以上代码,我们可以实现通过鼠标的左右移动来控制物体旋转,同时通过鼠标的滚轮滚动来控制物体的缩放。 最后,将脚本组件添加到旋转控制器上,然后运行 Unity 场景,就可以通过鼠标旋转和缩放物体了。 以上就是使用 Unity 创建一个可以实现物体360旋转缩放的简单示例。这个方法可以应用于虚拟现实、游戏开发以及各种仿真应用中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值