3D数学,hit.normal击中目标的法线位置 + 平时常用的小东西

  float f = Vector3.Angle( Vector3 f3,Vector3 f4 );

用于判断两个向量之间的夹角

float ff = Vector3.Distance(f3.position, f4.position);

用于计算两点之间的距离 

//求角度 及前后左右方位  
    public void Dis(Transform target)  
    {  
        //xuqiTest:  target.position = new Vector3(3, 0, 5);  
        Vector3 dir = target.position - transform.position; //位置差,方向  
 
        //方式1   点乘  
        //点积的计算方式为: a·b =| a |·| b | cos < a,b > 其中 | a | 和 | b | 表示向量的模 。  
        float dot = Vector3.Dot(transform.forward, dir.normalized);//点乘判断前后:dot >0在前,<0在后
        float dot1 = Vector3.Dot(transform.right, dir.normalized);//点乘判断左右: dot1>0在右,<0在左
        float angle = Mathf.Acos(Vector3.Dot(transform.forward.normalized, dir.normalized)) * Mathf.Rad2Deg;//通过点乘求出夹角  
      
        //方式2   叉乘  
        //叉乘满足右手准则  公式:模长|c|=|a||b|sin    
        Vector3 cross = Vector3.Cross(transform.forward, dir.normalized);//叉乘判断左右:cross.y>0在左,<0在右   
        Vector3 cross1 = Vector3.Cross(transform.right, dir.normalized); //叉乘判断前后:cross.y>0在前,<0在后   
        angle = Mathf.Asin(Vector3.Distance(Vector3.zero, Vector3.Cross(transform.forward.normalized, dir.normalized))) * Mathf.Rad2Deg;  
          

你想要获取平面上的夹角,可以忽略物体的上下方向,只考虑它们在水平面上的方向向量。你可以使用以下代码来获取水平面上的方向向量:

Vector3 directionAHoriz = new Vector3(directionA.x, 0f, directionA.z).normalized; Vector3 directionBHoriz = new Vector3(directionB.x, 0f, directionB.z).normalized;

判断玩家是否处于上下坡

    public void DownRoad()
    {
        // 从玩家位置发射射线朝下检测斜坡
        Ray slopeRay = new Ray(transform.position, Vector3.down);
        RaycastHit hit;

        if (Physics.Raycast(slopeRay, out hit))
        {

            if (hit.collider.name.Equals("Road 16"))
            {
                // 检测到碰撞物体
                Vector3 slopeNormal = hit.normal;
                float slopeAngle = Vector3.Angle(slopeNormal, Vector3.forward);
                float f = Mathf.Abs(slopeAngle - 90);
                Debug.Log(slopeAngle);
                // 如果夹角大于0度则为下坡
                if (slopeAngle - 90 > 0)
                {
                    transform.rotation = Quaternion.Euler(90-slopeAngle, 0, 0);
                    if (slopeAngle - 90 > 25)
                    {
                        transform.rotation = Quaternion.Euler(-25, 0, 0);
                    }
                }
                else if (slopeAngle - 90<0)
                {
                    transform.rotation = Quaternion.Euler(90-slopeAngle , 0, 0);
                    if (slopeAngle - 90 < -25)
                    {
                        transform.rotation = Quaternion.Euler(25, 0, 0);
                    }
                }
                else if (f<3.0f && f>0.0f)
                {
                    transform.rotation = Quaternion.Euler(0, 0, 0);
                }
            }  
        }
    }

拖动物体移动

   private void OnMouseDrag()
    {
        //通过鼠标扯拽控制刚体移动
        transform.position += Vector3.right * speed * Time.deltaTime * Input.GetAxis("Mouse X");
        transform.position += Vector3.up * speed * Time.deltaTime * Input.GetAxis("Mouse Y");
        transform.position += Vector3.forward * speed * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel");
    }

通过实现OnCollisionEnter函数来获取碰撞事件,在该函数中可以获取到碰撞信息:

hit.normal法线:

例如子弹击中物体时,会在物体表面生成一个弹孔,该弹孔的朝向可以通过normal法线向量进行设置,同样,击中物体时产生的溅射粒子特效的朝向也需要根据该法线信息进行设置。

具体解析可看一下博主:

Unity【RaycastHit】- 关于射线投射碰撞信息中normal法线向量的运用_unity hit.point - hit.normal_CoderZ1010的博客-CSDN博客

void OnCollisionEnter(Collision collision)
{
    Vector3 collisionForce = collision.impulse / Time.fixedDeltaTime;
    Debug.Log("Collision Force: " + collisionForce);
}

通过发射一条射线,检测射线与物体的碰撞信息,可以获取碰撞的法线方向和碰撞点等信息。

RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
    Vector3 hitForceDirection = hit.normal;
    Debug.Log("Hit Force Direction: " + hitForceDirection);
}

文本,图片在三维场景里面始终看向玩家相机

private Text enemy;

enemy=查找目标玩家的位置的Text文本存放位置

//血条看向玩家
        Vector3 cameraDirection = Camera.main.transform.forward;
        cameraDirection.y = 0f;

        //存在时调用
        enemy.transform.rotation = Quaternion.LookRotation(cameraDirection);

猜你喜欢

转载自blog.csdn.net/m0_71624363/article/details/132027150