zoukankan      html  css  js  c++  java
  • Unity3D笔记 英保通十 射线碰撞器检测

      射线碰撞检测可以用来检测方向和距离: 通过Physics.RayCast光线投射来实现:常用于射击利用发射的射线来判断、还有对战中刀剑交战中。。

    一、要涉及到RayCast和RayCastHit

    1.1、光线投射:Physics.RayCast()

      static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool 

    返回值也是bool

    • origin:在世界坐标,射线的起始点。
    • direction:射线的方向
    • distance:射线的长度
    • layerMask:只选定Layermask层内的碰撞器,其它层内碰撞器忽略。 

    1.2、RayCastHit() 光线投射检测

      用来获取从raycast函数中得到的信息反馈的结构。

    二、案例

    using UnityEngine;
    using System.Collections;
    
    public class rayCast : MonoBehaviour {
    
        private RaycastHit rayHit;//光线投射碰撞:用来获取从raycast函数中得到的信息反馈的结构。
    
        // Use this for initialization
        void Start () {
        
        }
        
        // Update is called once per frame
        void Update () {
            if (Physics.Raycast(transform.position, transform.forward,out rayHit, 10))
            {
                Debug.Log("碰撞检测到第一人称碰撞到了"+rayHit.collider.name);
            }
        }
    }

     RayCastHit参数:

      • The impact point in world space where the ray hit the collider.
        在世界空间中,射线碰到碰撞器的碰撞点。
      • The normal of the surface the ray hit.
        射线所碰到的表面的法线。
      • The barycentric coordinate of the triangle we hit.
        所碰到的三角形的重心坐标。
      • The distance from the ray's origin to the impact point.
        从光线的原点到碰撞点的距离。
      • The index of the triangle that was hit.
        碰到的三角形的索引。
      • The uv texture coordinate at the impact point.
        在碰撞点的UV纹理坐标。
      • The secondary uv texture coordinate at the impact point.
        碰撞点的第二个UV纹理坐标。
      • The uv lightmap coordinate at the impact point.
        所在碰撞点的光照图UV坐标。
      • The Collider that was hit.
        碰到的碰撞器。
      • The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.
        碰到的碰撞器的Rigidbody。如果该碰撞器没有附加刚体那么它为null。
      • The Transform of the rigidbody or collider that was hit.
        碰到的刚体或碰撞器的变换。
  • 相关阅读:
    Cocos2Dx for XNA类解析(1): CCApplication
    struts2动态调用action的方法
    python导出依赖包
    python 字符串split()方法
    struts2使用通配符调用action
    python3重新编译
    Jquery中html()、text()、val()的使用和区别
    Javascript写在<body>和<head>中的区别
    设计模式Design Pattern(2)单例模式
    设计模式Design Pattern(1)简介
  • 原文地址:https://www.cnblogs.com/PEPE/p/3604399.html
Copyright © 2011-2022 走看看