[Unity]带旋转方向的摄像机移动区域限制

using UnityEngine;

public class ObliqueCameraBox
{
    private Vector2Int mPoint0; // 左下角
    private Vector2Int mPoint1; // 右上角
    private int mRotation; // 旋转值

    private float mRotationRad
    {
        get
        {
            return Mathf.Deg2Rad * mRotation;
        }
    }

    public ObliqueCameraBox(Vector2Int point0, Vector2Int point1, int rotation)
    {
        mPoint0 = point0;
        mPoint1 = point1;
        mRotation = rotation;
    }
    
    static Vector2 RotateXZ2D(Vector2 point, Vector2 center, float a)
    {
        float x = ( point.x - center.x ) * Mathf.Cos(a) - ( point.y - center.y ) * Mathf.Sin(a) + center.x;
        float z = ( point.x - center.x ) * Mathf.Sin(a) + ( point.y - center.y ) * Mathf.Cos(a) + center.y;
        return new Vector2(x, z);
    }

    public bool Limit(Vector2 input, int damping, ref Vector2 output)
    {
        var p = RotateXZ2D(input, mPoint0, mRotationRad );

        int c = 0;

        if (p.x > mPoint1.x - damping)
        {
            p.x = mPoint1.x - damping; 
        }
        else if (p.x < mPoint0.x + damping)
        {
            p.x = mPoint0.x + damping;
        }
        else
        {
            ++c;
        }

        if (p.y > mPoint1.y - damping)
        {
            p.y = mPoint1.y - damping;
        }
        else if (p.y < mPoint0.y + damping)
        {
            p.y = mPoint0.y + damping;
        }
        else
        {
            ++c;
        }

        if (c == 2)
        {
            return false;
        }
        else
        {
            output = RotateXZ2D(p, mPoint0, -mRotationRad );
            return true;
        }
    }
}

原文链接地址:http://blog.coolcoding.cn/?p=1844

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值