向量是有方向和大小的量,所谓单位化就是保持其方向不变,将其长度化为1  有一向量a(标箭头),其长度为绝对值a,单位化为(a/绝对值a)

公式:

向量a的坐标为(x,y),那么其长度(又称为模)为:√(x²+y²)。单位化后为(x,y)/√(x²+y²)或(x/√(x²+y²) , y/√(x²+y²))

 

代码实现方式:

public void normalized2D(Vector3 pos) 
    {
        Vector3 temp = Vector3.zero;

        temp.x = (float)(pos.x / (Math.Sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z)));
        temp.y = (float)(pos.y / (Math.Sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z)));
        temp.z = (float)(pos.z / (Math.Sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z)));

        Debug.Log(temp);
    }


    public void normalized3D(Vector2 pos)
    {
        Vector2 temp = Vector2.zero;

        temp.x = (float)(pos.x / (Math.Sqrt(pos.x * pos.x + pos.y * pos.y)));
        temp.y = (float)(pos.y / (Math.Sqrt(pos.x * pos.x + pos.y * pos.y)));

        Debug.Log(temp);
    }

相关文章:

  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2022-12-23
  • 2021-06-24
  • 2021-07-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-15
  • 2021-12-12
  • 2021-10-31
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案