【问题标题】:Unity mathf.clamp in multiple angle rangesunity mathf.clamp 在多个角度范围内
【发布时间】:2020-11-18 12:51:45
【问题描述】:

我想将箭头移动的区域限制在 -140 到 -40 度之间,如图所示,但是如果我将其限制在 -140 到 -40 度之间,角度区域的度数分布很奇怪分别是最小值和最大值,它只让我在红色区域移动箭头,而不是在所需的区域,我的意思是我只想在非红色区域移动箭头,如果有人可以帮助我从这段代码开始而不改变非常感谢,谢谢。

编辑:添加了基于条件的临时代码。

explanatory diagram

    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    difference.Normalize();
    float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    //rotationZ = Mathf.Clamp(rotationZ, -140, 180);
    if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
    if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
    transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
    
    Debug.Log(rotationZ);
    if (rotationZ < -90 || rotationZ > 90)
        {

        if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
        if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
        if (myPlayer.transform.eulerAngles.y == 0)
            {

                transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);


            }
            else if (myPlayer.transform.eulerAngles.y == 180)
            {
                transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
            }


        }
    }
}

【问题讨论】:

  • 它是3D游戏吗?功能就像在Rust中按Alt环顾四周,所以角色不会向后看180度?
  • 我不喜欢这段代码,但至少你可以使用if(rotationZ &lt;= -90 &amp;&amp; rotationZ &gt; -140) {rotationZ = -140;} if(rotationZ &gt; -90 &amp;&amp; rotationZ &lt; -40) {rotationZ = -40;}
  • @Whitebrim 你会做什么呢?我有兴趣学习一种更有效的方法。我附上了完整的代码,因为还有一部分我改变了图像的旋转,使其成为镜像版本。
  • 将 -140 改为 220
  • @Antoine 是的,但它是一个圆度方程,只是一个简单的技巧,Unity 通过加减 360 度来转换它,这意味着 220 等于 -140,这个转换将使你的箭头在非红色区域从 -140 移动到 -40。试试吧 rotationZ = Mathf.Clamp(rotationZ, -40, 220);

标签: visual-studio unity3d rotation 2d euler-angles


【解决方案1】:
private readonly float deadZone = Mathf.Sin(-40 * Mathf.Deg2Rad);
private Vector3 rightDeadZoneEnd = new Vector3(Mathf.Cos(-40 * Mathf.Deg2Rad), Mathf.Sin(-40 * Mathf.Deg2Rad), 0);
private Vector3 leftDeadZoneEnd = new Vector3(Mathf.Cos(-140 * Mathf.Deg2Rad), Mathf.Sin(-140 * Mathf.Deg2Rad), 0);

private void Update()
{
    Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    direction.Normalize();
        
    if (direction.y < deadZone)
    {
        if (direction.x >= 0)
        {
            direction = rightDeadZoneEnd;
        }
        else
        {
            direction = leftDeadZoneEnd;
        }
    }

    if (direction.x < 0)
    {
        transform.localScale = new Vector3(1, -1, 1);
    }
    else
    {
        transform.localScale = Vector3.one;
    }

    direction = Quaternion.Euler(0, 0, 90) * direction;

    transform.localRotation = Quaternion.LookRotation(Vector3.forward, direction);
}

【讨论】:

    【解决方案2】:
        void FixedUpdate()
        {
            
            Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) 
            - transform.position;
            difference.Normalize();
            float rotationZ = Mathf.Atan2(difference.y, difference.x) * 
            Mathf.Rad2Deg;
    
            if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
            else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
    
            transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
    
            if (rotationZ < -90 || rotationZ > 90)
            {
                if (rotationZ <= -90 && rotationZ > -140) { rotationZ = -140; }
                else if (rotationZ > -90 && rotationZ < -40) { rotationZ = -40; }
    
                if (myPlayer.transform.eulerAngles.y == 0)
                {
                        transform.localRotation = Quaternion.Euler(180, 0, - 
                        rotationZ);
                }
                else if (myPlayer.transform.eulerAngles.y == 180)
                {
                        transform.localRotation = Quaternion.Euler(180, 180, - 
                        rotationZ);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多