【问题标题】:How to limit angles of a vector?如何限制向量的角度?
【发布时间】:2016-07-24 19:36:59
【问题描述】:

我尝试编写一个脚本来显示手指跟随的精灵。但我需要这个精灵只能在距锚点的指定距离上移动,并且只能在指定角度之间移动。现在,我有下一个代码:

public class GunPowerController : MonoBehaviour {

    public GameObject _fingerprint;
    public Transform _anchor;
    public Gun _gun;
    public float _maxPower = 1f;
    public float _maxAngle = 15f;
    public float _minAngle = 0f;

    private Camera _camera;
    private GameObject _fingerprintInstance;

    void Awake()
    {
        _maxPower = Mathf.Abs(_maxPower);
        _camera = Camera.main;
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var touchWorldPosition = _camera.ScreenToWorldPoint(Input.mousePosition);
            var hitInfo = Physics2D.Raycast(touchWorldPosition, Vector2.zero);

            if (hitInfo && hitInfo.transform.gameObject.Equals(gameObject))
            {
                touchWorldPosition.z = transform.position.z;
                _fingerprintInstance = (GameObject)Instantiate(_fingerprint, touchWorldPosition, Quaternion.identity);
            }
        }
        else if (_fingerprintInstance != null)
        {

            if (Input.GetMouseButtonUp(0))
            {
                Destroy(_fingerprintInstance);
            }
            else
            {
                var touchWorldPosition = _camera.ScreenToWorldPoint(Input.mousePosition);
                Move(touchWorldPosition);
            }
        }
    }

    private void Move(Vector3 target)
    {
        target.z = transform.position.z;
        Vector3 distance = target - _anchor.position;
        Vector3 axis = _anchor.position;
        axis.x = -1f;
        float angle = Vector3.Angle(axis, distance) * Mathf.Sign(distance.y - axis.y);

        if (distance.sqrMagnitude > _maxPower * _maxPower)
        {
            distance.Normalize();
            distance *= _maxPower;
            target = _anchor.position + distance;
        }

        if(_minAngle > angle)
        {
            //Here I need to hold vector rotation while the user doesn't
            //return to the available space between angles.
        }
        else if (_maxAngle < angle)
        {
            //Here I need to hold vector rotation while the user doesn't
            //return to the available space between angles.
        }

        _fingerprintInstance.transform.position = Vector3.Lerp(_fingerprintInstance.transform.position, target,
            10f * Time.deltaTime);
    }
}

我尝试在 _min|maxAngle - angle 的差异上旋转矢量 target,但它工作错误。如何制作?
目前的问题:
附言我重试了很多变种,但都没有成功。如果需要一些详细信息,请写信给我,我会发布。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    你应该使用Mathf.Clamp

    链接:https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

    【讨论】:

    • 我知道如何限制人数。我不知道如何为矢量角制作它
    • 矢量长度可以随时更改。
    • Mathf.Clamp(transform.rotation.y, 0, 15);
    • 谢谢,我不知道!
    • 不行!据我了解,它限制了对象旋转,但我需要限制矢量旋转。请看我的照片。指纹永远不会旋转,只会在计算出的位置上移动。
    猜你喜欢
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多