版本 |
作者 |
参与者 |
完成日期 |
备注 |
UnityAPI_Physics_V01_1.0 |
严立钻 |
|
2020.05.21 |
|
|
|
|
|
|
立钻哥哥:Unity是一个入门快、提高难的游戏引擎,想要提升能力,至少需要越过3道坎:API+Shader+综合能力;++1、API的积累:对API的合理利用不仅可以减轻自己的编码负担,而且往往可以提高程序的运行效率;这也是钻哥开始“Unity API”独立打造分类的初衷; ++2、Shader编程:想要做出一款精品游戏往往需要有高效的Shader的支持;Unity提供了一套改良的“Shader Lab”系统,优化了繁杂的“Open GL”编程; ++3、综合能力(技术+业务+管理):一款产品的制作除了功能编程外,往往会涉及很多其他领域,例如产品架构、UI交互设计、模型制作等,作为主要的编程人员,对其他相关领域的了解程序往往会影响到产品制作直至最后的产品体验; ++++立钻哥哥一直在推动【VR云游戏=Unity+SteamVR+云技术+5G+AI】,这个只是一个引子,抛砖引玉让大家对整个知识体系有一个明确的落地方向,宝宝们可以根据自己的兴趣方向进行拓展:比如Unity这里是指一种“3D游戏引擎”,也可拓展至“UE4、Cocos2dx”等游戏引擎;SteamVR是一种跨硬件解决方案,也可拓展至“VRTK”等第三方插件;“云技术+5G”是一种跨平台解决方案的核心技术,同样可拓展至其他平台解决方案;AI也是一种先进技术的举例,也可以拓展至任何一种前沿技术; |
++++【Unity API】分类:https://blog.csdn.net/vrunsoftyanlz/category_7637520.html
++++【Unity开发基础】分类:https://blog.csdn.net/vrunsoftyanlz/category_7309057.html
++++【Linux系统编程】分类:https://blog.csdn.net/vrunsoftyanlz/category_9694767.html
++++【C++C铸就生存利器】分类:https://blog.csdn.net/vrunsoftyanlz/category_9325802.html
++++【人工智能AI2026】分类:https://blog.csdn.net/vrunsoftyanlz/category_9212024.html
++++【立钻哥哥CSDN空间】:https://blog.csdn.net/VRunSoftYanlz/
#Physics物理 |
#Physics物理++A1、Description描述++B2、Variables变量++C3、Public Function共有函数++D4、Message消息 |
#A1、Description描述 |
立钻哥哥:Global physics properties and helper methods. |
#B2、Static Variables静态变量 |
++B2、Static Variables静态变量++++B2.1、AllLayers++++B2.2、bounceThreshold++++B2.3、defaultContactOffset++++B2.4、DefaultRaycastLayers++++B2.5、gravity++++B2.6、IgnoreRaycastLayer++++B2.7、queriesHitTriggers++++B2.8、sleepThreshold++++B2.9、solverIterationCount++++B2.10、YanlzXREngine.Physics.StaticVariables |
++B2.1、AllLayers |
public static int AllLayers; |
++++选择所有层的层蒙版常量;
++++可用于Physics.Raycast的层蒙版字段以及其他,来选择所有层;
++B2.2、bounceThreshold |
public static float bounceThreshold; |
++++两个碰撞物体的相对速度如果在这个设定值之下,不会反弹;默认值2;取值范围0到无穷大;必须为正数;
++++这个值在【Edit->Project Settings->Physics】菜单下设定;而不用在脚本中;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{ void MyTestFunc(){ Physics.bounceThreshold = 1; } } //立钻哥哥:public class YanlzPhysics{} |
++B2.3、defaultContactOffset |
public static float defaultContactOffset; |
++++新创建碰撞器的默认接触偏移值;
++++那些碰撞器间的距离比它们的contactOffset值之和小将产生接触;该触点偏移必须为正;触点偏移允许碰撞检测系统预判接触,即使对象稍微分开点;
++B2.4、DefaultRaycastLayers |
public static int DefaultRaycastLayers; |
++++选择默认投射层的层蒙版常数;
++++可用于Physics.Raycast的层蒙版字段以及其他,来选择默认投射层;该默认层是除了忽略层的所有层;
++B2.5、gravity |
public static Vector3 gravity; |
++++场景中应用到所有刚体的重力;
++++使用useGravity属性,可以单个开关刚体重力;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{ void MyTestFunc(){ Physics.gravity = new Vector3(0, -1.0F, 0); } } //立钻哥哥:public class YanlzPhysics{} |
++B2.6、IgnoreRaycastLayer |
public static int IgnoreRaycastLayer; |
++++选择忽略层的层蒙版常量;
++++可用于Physics.Raycast的层蒙版字段以及其他,来选择忽略层(默认不接受投射);
++B2.7、queriesHitTriggers |
public static bool queriesHitTriggers; |
++++指定查询(raycasts, spherecasts, overlap tests, etc.)是否碰到触发器;
++++这能通过指定QueryTriggerInteraction参数,重写查询;
++B2.8、sleepThreshold |
public static float sleepThreshold; |
++++质量归一化的能量阀值,哪个对象开始休眠;
++B2.9、solverIterationCount |
public static int solverIterationCount; |
++++默认的求解迭代次数允许使用任意刚体(默认为7);必须为正数;(可以纠正关节间连接的错误行为);
++++solverIterationCount决定了关节和接触点有多精确;可以纠正关节间连接的错误行为;如果关节刚体有摇摆现象和行为杂乱,为solverIterationCount设置一个较高的值将改善他们的稳定性;通常情况下设置为7;
++++这个值在【Edit->Project Settings->Physics】菜单下设定,而不同在脚本中;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzGameObject : MonoBehaviour{ void MyTestFunc(){ Physics.solverIterationCount = 10; } } //立钻哥哥:public class YanlzGameObject{} |
#C3、Static Functions静态函数 |
++C3、Variables变量++++C3.1、BoxCast++++C3.2、BoxCastAll++++C3.3、BoxCastNonAlloc++++C3.4、CapsuleCast++++C3.5、CapsuleCastAll++++C3.6、CapsuleCastNonAlloc++++C3.7、CheckBox++++C3.8、CheckCapsule++++C3.9、CheckSphere++++C3.10、GetIgnoreLayerCollision++++C3.11、IgnoreCollision++++C3.12、IgnoreLayerCollision++++C3.13、Linecast++++C3.14、OverlapBox++++C3.15、OverlapBoxNonAlloc++++C3.16、OverlapSphere++++C3.17、OverlapSphereNonAlloc++++C3.18、Raycast++++C3.19、RaycastAll++++C3.20、RaycastNonAlloc++++C3.21、SphereCast++++C3.22、SphereCastAll++++C3.23、SphereCastNonAlloc++++C3.24、YanlzXREngine.Physics.StaticFunctions |
++C3.1、BoxCast |
public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation=Quaternion.identity, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, out RaycastHit hitInfo, Quaternion orientation = Quaternion.identity, float maxDistance=Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[center]:盒中心;
++++[halfExtents]:盒的每轴的一半大小;
++++[direction]:投射盒的方向;
++++[orientation]:盒的旋转;
++++[maxDistance]:投射的最大长度;
++++[layerMask]:选择投射的碰撞器层蒙版;
++++[queryTriggerInteraction]:指定查询碰到触发器;
++++[hitInfo]:包含更多的碰到碰撞器的信息;
++++[返回值]:如果发现交叉,返回true;
++++沿射线投射立方体,并返回碰到的信息;
++C3.2、BoxCastAll |
public static RaycastHit[] BoxCastAll(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation=Quaternion.identity, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[center]:盒的中心;
++++[halfExtents]:盒的每轴的一半大小;
++++[direction]:投射盒的方向;
++++[orientation]:盒的旋转;
++++[maxDistance]:投射的最大长度;
++++[layerMask]:选择投射的碰撞器层蒙版;
++++[queryTriggerInteraction]:指定查询碰到触发器;
++++[返回值]:返回碰到的所有碰撞器的RaycastHit[];
++++就像Physics.BoxCast,反返回的是所有碰到的对象信息;
++C3.3、BoxCastNonAlloc |
public static int BoxCastNonAlloc(Vector3 center, Vector3 halfExtents, Vector3 direction, RaycastHit[] results, Quaternion orientation=Quaternion.identity, float maxDistance=Mathf.Infinity, int layermask=DefaultRaycastLayers, QueryTriggerInteraction queryInteraction=QueryTriggerInteraction.UseGlobal); |
++++[center]:盒中心;
++++[halfExtents]:盒每轴的一半大小;
++++[direction]:盒投射方向;
++++[results]:储存结果的缓冲器;
++++[orientation]:盒的旋转;
++++[maxDistance]:投射的最大长度;
++++[layermask]:选择投射的层蒙版;
++++[queryTriggerInteracion]:指定是否查询碰到触发器;
++++[返回值]:返回碰到的储存在results缓冲器中的数量;
++++沿方向投射立方体,并储存到提供的缓冲器;
++C3.4、CapsuleCast |
public static bool CapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[point1]:球体开始中心点;
++++[point2]:球体结束的中心点;
++++[radius]:胶囊的半径;
++++[direction]:胶囊扫描的方向;
++++[maxDistance]:扫描的最大长度;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++C3.5、CapsuleCastAll |
public static RaycastHit[] CapsuleCastAll(Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[point1]:球体开始中心点;
++++[point2]:球体结束的中心点;
++++[radius]:胶囊的半径;
++++[direction]:胶囊扫描的方向;
++++[maxDistance]:扫描的最大长度;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[返回值]:返回扫描到的所有碰撞器RaycastHit信息;
++++像Physics.CapsuleCast,但这个函数将返回所有胶囊扫描交叉碰到的对象;
++++投射一个胶囊针对场景中所有碰撞器,并返回所有碰到的每个碰撞器的细节信息;胶囊是由point1和point2位置的两个球带有radius半径形成胶囊的两端定义;返回胶囊沿direction方向碰到的第一个碰撞器;这通常用于投射不需足够的精度,因为要找出一个特定大小的物体,如人物,能移动到某处而不在途中碰撞到任何东西;
++++如果从脚本或动画移动碰撞器,需要有至少一个在FixedUpdate中执行,使得物理库可以更新它的数据结构,在CapsuleCast碰到碰撞器之前是在它的新位置;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{
void Update(){ RaycastHit[] hits; CharacterController charCtrl = GetComponent<CharacterController>(); Vector3 p1 = transform.position + charCtrl.center + Vector3.up * -charCtrl.height * 0.5F; Vector3 p2 = p1 + Vector3.up * charCtrl.height;
//Cast character controller shape 10 meters forward, to see if it is about to hit anything hits = Physics.CapsuleCastAll(p1, p2, charCtrl.radius, transform.forward, 10);
//Change the material of all hit colliders to use a transparent Shader for(int i = 0; i < hits.Length; i++){ RaycastHit hit = hits[i]; Renderer rend = hit.transform.GetComponent<Renderer>();
if(rend){ rend.material.shader = Shader.Find(“Transparent/Diffuse”); Color tempColor = rend.material.Color; tempColor.a = 0.3F; rend.material.color = tempColor; } //立钻哥哥:if(){} } //立钻哥哥:for(){}
} //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.6、CapsuleCastNonAlloc |
public static int CapsuleCastNonAlloc(Vector3 point1, Vector3 point2, float radius, Vector3 direction, RaycastHit[] results, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[point1]:球体开始中心点;
++++[point2]:球体结束的中心点;
++++[radius]:胶囊的半径;
++++[direction]:胶囊扫描的方向;
++++[results]:缓冲器用来储存碰到的信息;
++++[maxDistance]:扫描的最大长度;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[返回值]:返回碰到的对象储存在缓冲器中的数量;
++++针对场景中所有的碰撞器投射一个胶囊,并返回缓冲器中碰到什么的信息;
++++像Physics.CapsuleCastAll,但不产生垃圾信息;
++C3.7、CheckBox |
public static bool CheckBox(Vector3 center, Vector3 halfExtents, Quaternion orientation=Quaternion.identity, int layermask=DefaultRaycastLayer, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[center]:盒的中心;
++++[halfExtents]:盒每轴的一半大小;
++++[orientation]:盒的旋转;
++++[layermask]:旋转投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[返回值]:如果盒重叠任意碰撞器,返回true;
++++检测给定盒是否与其他碰撞器重叠;
++C3.8、CheckCapsule |
public static bool CheckCapsule(Vector3 start, Vector3 end, float radius, int layermask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[start]:胶囊开始处的球中心;
++++[end]:胶囊结束处的球中心;
++++[radius]:胶囊的半径;
++++[layermask]:选择胶囊投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++在世界坐标空间,检查胶囊体是否与任意碰撞器重叠;
++++胶囊是由radius半径与point1和point2位置的两个球形成胶囊的两端定义;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{ bool CorridorIsWideEnough(Vector3 startPt, Vector3 endPt, float width){ return Physics.CheckCapsule(startPt, endPt, width); } } //立钻哥哥:public class YanlzPhysics{} |
++C3.9、CheckSphere |
public static bool CheckSphere(Vector3 position, float radius, int layermask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[position]:球的中心;
++++[radius]:球的中心;
++++[layermask]:选择胶囊投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++在世界坐标空间,由位置和半径定义的球与任意碰撞器重叠,如果重叠则返回true;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{ public float sphereRadius; AudioSource audio;
void Start(){ audio = GetComponent<AudioSource>(); } //立钻哥哥:void Start(){}
void MyWarningNoise(){ //Play a noise if an object is within the sphere’s radius if(Physics.CheckSphere(transform.position, sphereRadius)){ audio.Play(); } } //立钻哥哥:void MyWarningNoise(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.10、GetIgnoreLayerCollision |
public static bool GetIgnoreLayerCollision(int layer1, int layer2); |
++++layer1和layer2的层忽略了吗?
++++返回Physics.IgnoreLayerCollision或在物理检视面板设置的值;
++C3.11、IgnoreCollision |
public static void IgnoreCollision(Collider collider1, Collider collider2, bool ignore=true); |
++++使碰撞检测系统忽略所有collider1和collider2碰撞;
++++这用于,比如说,防止炮弹与发射的对象碰撞(避免炮弹与炮筒碰撞);
++立钻哥哥:IgnoreCollision有一些局限性:++++1)不具有持续性,这意味着忽略的碰撞状态当场景保存时,在编辑器中不会储存;++++2)只能在运行时应用忽略碰撞到碰撞器;当禁用该碰撞器或刚体,IgnoreCollision状态将丢失,必须再次调用Physics.IgnoreCollision; |
++++注意:IgnoreCollision将重置受影响的碰撞器的触发状态,因此可能收到OnTriggerEnter和OnTriggerExit的响应消息;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{ public Transform bulletPrefab;
void Start(){ Transform bullect = Instantiate(bulletPrefab) as Transform; Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>()); } //立钻哥哥:void Start(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.12、IgnoreLayerCollision |
public static void IgnoreLayerCollision(int layer1, int layer2, bool ignore=true); |
++++使碰撞检测系统忽略layer1和layer2中的任意碰撞器的所有碰撞;
++++注意:IgnoreLayerCollision将重置受影响碰撞器的触发状态,因此可能收到OnTriggerEnter和OnTriggerExit的响应消息;
++++可以在物理检视面板为项目设置任意层组合的碰撞;
++C3.13、Linecast |
public static bool Linecast(Vector3 start, Vector3 end, int layerMask=DefaultRaycastLayers);public static bool Linecast(Vector3 start, Vector3 end, out RaycastHit hitInfo, int layerMask=DefaultRaycastLayers); |
++++在线的开始和结束位置之间,如果与任何碰撞器相交返回真;
++++Layer mask是用来指定投射的层;
++++如果返回true,hitInfo返回碰到的RaycastHit信息;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{ public Transform target;
void Update(){ if(!Physics.Linecast(transform.position, target.position)){ } } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.14、OverlapBox |
public static Collider[] OverlapBox(Vector3 center, Vector3 halfExtents, Quaternion orientation=Quaternion.identity, int layerMask=AllLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[center]:盒中心;
++++[halfExtents]:盒每轴大小的一半;
++++[orientation]:盒的旋转;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[返回值]:返回与给定立方体重叠的所有碰撞器;
++++查找给定立方体内或与之接触的所有碰撞器;
++C3.15、OverlapBoxNonAlloc |
public static int OverlapBoxNonAlloc(Vector3 center, Vector3 halfExtents, Collider[] results, Quaternion orientation=Quaternion.identity, int layerMask=AllLayers, Quaternion queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[center]:盒中心;
++++[halfExtents]:盒每轴大小的一半;
++++[results]:储存结果到缓冲器;
++++[orientation]:盒的旋转;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[返回值]:返回储存在results中碰撞器的数量;
++++查找给定立方体内或与之接触的所有碰撞器,并储存在缓冲器中;
++C3.16、OverlapSphere |
public static Collider[] OverlapSphere(Vector3 position, float radius, int layerMask=AllLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[position]:球体中心;
++++[radius]:球的半径;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++返回球内或与之接触的所有碰撞器;
++++注意:这个仅针对碰撞器的边界体检测,而不是针对实际的碰撞器;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{
void MyExplosionDamage(Vector3 center, float radius){ Collider[] hitColliders = Physics.OverlapSphere(center, radius);
int i = 0; while(i < hitColliders.Length){ hitColliders[i].SendMessage(“MyAddDamage”); i++; } } //立钻哥哥:void MyExplosionDamage(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.17、OverlapSphereNonAlloc |
public static int OverlapSphereNonAlloc(Vector3 position, float radius, Collider[] results, int layerMask=AllLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[position]:球体中心;
++++[radius]:球的半径;
++++[results]:储存结果到缓冲器;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[返回值]:返回储存在results缓冲器中碰撞器的数量;
++++计算并储存该球内或与之接触的所有碰撞器到提供的缓冲器中;
++++像Physics.OverlapSphere,但不产生垃圾;
立钻哥哥:Like Physics.OverlapSphere, but generates no garbage. |
++C3.18、Raycast |
public static bool Raycast(Vector3 origin, Vector3 direction, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal);public static bool Raycast(Ray ray, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal);public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[origin]:在世界坐标,射线的起始点;
++++[direction]:射线的方向;
++++[distance]:射线的长度;
++++[layermask]:投射射线,选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[hitInfo]:将包含碰到碰撞器的更多信息;
++++[ray]:射线的起点和方向;
++++[返回值]:当光线投射与任何碰撞器交叉时为真,否则为假;
++++在场景中投下可与所有碰撞器碰撞的一条光线;
++++使用ray.origin和ray.direction替代origin和direction;
++++注意:如果射线从碰撞体的内部或者背面打,Raycast不检测碰撞;如果用脚本或动画移动碰撞器,需要至少一个在FixedUpdate执行,因为物理库能更新它的数据结构,因此一条射线在它的新位置之前打到碰撞器;
using UnityEngine; using System.Collections; using YanlzREngine;
public class YanlzPhysics : MonoBehaviour{
void Update(){ Vector3 fwd = transform.TransformDirection(Vector3.forward); if(Physics.Raycast(transform.position, fwd, 10)){ Debug.Log(“立钻哥哥:There is something in front of the object!”); } } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics{}
|
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{
void Update(){ RaycastHit hit;
//if(Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F)){} if(Physics.Raycast(trasform.position, -Vector3.up, out hit)){ float distanceToGround = hit.distance; } } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics : MonoBehaviour{}
|
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{
void Update(){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, 100)){ Debug.Log(“立钻哥哥:Hit something!”); } } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics{}
|
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{
void Update(){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100)){ Debug.DrawLine(ray.origin, hit.point); } } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.19、RaycastAll |
++++立钻哥哥:射线投射列表;
public static RaycastHit[] RaycastAll(Ray ray, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal);public static RaycastHit[] RaycastAll(Vector3 origin, Vector3 direction, float maxDistance=Mathf.Infinity, int layermask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[ray]:带有开始点和方向的射线;
++++[maxDistance]:从射线开始允许投射的最大距离;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[origin]:射线的开始点;
++++[direction]:射线的方向;
++++在场景投射一条射线并返回所有碰撞;注意不保证顺序;
++++注意:射线投射不会从碰撞器内部检测碰撞;如果用脚本或者动画移动碰撞器,至少需要一个在FixedUpdate来执行物理库更新数据结构,Raycast在它的新位置之前碰到碰撞器;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{
void Update(){ RaycastHit[] hits; hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
for(int i = 0; i < hits.Length; i++){ RaycastHit hit = hits[i]; Renderer rend = hit.transform.GetComponent<Renderer>();
if(rend){ //Change the material of all hit colliders to use a transparent shader; rend.material.shader = Shader.Find(“Transparent/Diffuse”); Color tempColor = rend.material.color; tempColor.a = 0.3F; rend.material.color = tempColor; } //立钻哥哥:if(){} } //立钻哥哥:for(){} } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.20、RaycastNonAlloc |
public static int RaycastNonAlloc(Ray ray, RaycastHit[] results, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal);public static int RaycastNonAlloc(Vector3 origin, Vector3 direction, RaycastHit[] results, float maxDistance=Mathf.Infinity, int layermask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[ray]:带有开始点和方向的射线;
++++[results]:储存碰到对象的缓冲器;
++++[maxDistance]:从射线开始允许投射的最大距离;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[origin]:射线的开始点;
++++[direction]:射线的方向;
++++[返回值]:返回储存在results缓冲器中碰撞器的数量;
++++在场景投射射线并储存碰到的对象到缓冲器中;
++C3.21、SphereCast |
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal);public static bool SphereCast(Ray ray, float radius, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal);public static bool SphereCast(Ray ray, float radius, out RaycastHit hitInfo, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[origin]:扫描开始的球形中心点;
++++[radius]:球的半径;
++++[direction]:球形扫描的方向;
++++[hitInfo]:如果返回真,hitInfo将获取更多的碰撞信息;
++++[distance]:扫描的长度;
++++[layerMask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[ray]:球形扫描的射线;
++++[maxDistance]:最大距离;
++++[返回值]:当球形扫描与任意碰撞器相交,返回true;否则返回false;
++++沿着射线方向投射一个球形,并返回更多的碰到的信息;
++++可以用在射线投影无法满足要求的情况下;例如要找出一个特定大小的对象,比如角色能在某个地方移动,不与任何东西碰撞;球形投射就像有厚度的射线;在这种情况下射线是由开始向量和方向指定;
++++注意:SphereCast不检测球体重叠的碰撞器;球形投射不检测触发器;如果从脚本或动画移动碰撞器,至少需要一个FixedUpdate来执行物理库更新数据结构,球形投射在它的新位置之前碰到碰撞器;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysics : MonoBehaviour{ CharacterController charCtrl;
void Start(){ charCtrl = GetComponent<CharacterController>(); } //立钻哥哥:void Start(){}
void Update(){ RaycastHit hit; Vector3 p1 = transform.position + charCtrl.center; float distanceToObstacle = 0;
//Cast a sphere wrapping character controller 10 meters forward to see if it is about to hit anything; if(Physics.SphereCast(p1, charCtrl.height/2, transform.forward, out hit, 10)){ distanceToObstacle = hit.distance; } //立钻哥哥:if(){} } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysics{} |
++C3.22、SphereCastAll |
public static RaycastHit[] SphereCastAll(Vector3 origin, float radius, Vector3 direction, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers);public static RaycastHit[] SphereCastAll(Ray ray, float radius, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers); |
++++[origin]:扫描开始的球形中心点;
++++[radius]:球的半径;
++++[direction]:球形扫描的方向;
++++[distance]:扫描的长度;
++++[layerMask]:仅扫描指定层的碰撞器;
++++[ray]:球形扫描的射线;
++++[返回值]:返回扫描碰到的所有RaycastHit[]数组;
++++类似Physics.SphereCast,但是这个函数返回所有球形扫描到的碰撞信息;
++++在场景中球形投射扫描所有碰撞器,并返回碰到的每一个碰撞器的细节信息;这对于Raycast无法满足需求时使用,比如想找出指定大小的对象,比如角色能在某个地方移动,不与任何东西碰撞;
++++注意:SphereCastAll不检测球体重叠的碰撞器;球形投射不会检测触发器;如果从脚本或动画移动碰撞器,至少需要一个FixedUpdate来执行物理库更新数据结构,球形投射在它的新位置之前碰到碰撞器;
using UnityEngine; using System.Collections; using YanlzXREngine;
public class YanlzPhysicsSphereCastTest : MonoBehaviour{
void Update(){ RaycastHit hit; var hits = Physics.SphereCastAll(transform.position, 2, Vector3.forward, 10);
foreach(var item in hits){ Debug.Log(item.transform); } //立钻哥哥:foreach(){} } //立钻哥哥:void Update(){}
} //立钻哥哥:public class YanlzPhysicsSphereCastTest{} |
++C3.23、SphereCastNonAlloc |
pubic static int SphereCastNonAlloc(Vector3 origin, float radius, Vector3 direction, RaycastHit[] results, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal);public static int SphereCastNonAlloc(Ray ray, float radius, RaycastHit[] results, float maxDistance=Mathf.Infinity, int layerMask=DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal); |
++++[origin]:扫描开始球体的中心点;
++++[radius]:球体的半径;
++++[direction]:球体扫描的方向;
++++[results]:储存碰到对象的缓冲器;
++++[maxDistance]:扫描的最大长度;
++++[layermask]:选择投射的层蒙版;
++++[queryTriggerInteraction]:指定是否查询碰到触发器;
++++[ray]:带有开始点和方向的射线,用于球体扫描投射;
++++[返回值]:储存在results缓冲器中碰撞器的数量;
++++沿direction方向投射球体,并储存在results缓冲器中;
++++这个是Physics.SphereCastAll的变种,把查询的结果储存在提供的数组中;这个仅计算碰到对象的多少,储存到缓冲器中,并没有特定的顺序;它不能保证它只存储最近的碰撞;不产生垃圾;
#D4、立钻哥哥对Physics物理类的拓展 |
++++【Unity API】分类:https://blog.csdn.net/vrunsoftyanlz/category_7637520.html
++++【Unity开发基础】分类:https://blog.csdn.net/vrunsoftyanlz/category_7309057.html
++++【Linux系统编程】分类:https://blog.csdn.net/vrunsoftyanlz/category_9694767.html
++++【C++C铸就生存利器】分类:https://blog.csdn.net/vrunsoftyanlz/category_9325802.html
++++【人工智能AI2026】分类:https://blog.csdn.net/vrunsoftyanlz/category_9212024.html
++++【立钻哥哥CSDN空间】:https://blog.csdn.net/VRunSoftYanlz/
立钻哥哥推荐的拓展学习链接(Link_Url) |
++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/
++++虚拟现实VR资讯: https://blog.csdn.net/VRunSoftYanlz/article/details/89165846
++++HTC_VIVE开发基础:https://blog.csdn.net/VRunSoftYanlz/article/details/81989970
++++Oculus杂谈:https://blog.csdn.net/VRunSoftYanlz/article/details/82469850
++++Oculus安装使用:https://blog.csdn.net/VRunSoftYanlz/article/details/82718982
++++Unity+SteamVR=>VR:https://blog.csdn.net/VRunSoftYanlz/article/details/88809370
++++Unity减少VR晕眩症:https://blog.csdn.net/VRunSoftYanlz/article/details/89115518
++++SteamVR简介:https://blog.csdn.net/VRunSoftYanlz/article/details/86484254
++++SteamVR脚本功能分析:https://blog.csdn.net/VRunSoftYanlz/article/details/86531480
++++SteamVR2.0开发指南:https://blog.csdn.net/VRunSoftYanlz/article/details/86618187
++++SteamVR2.2.0开发指南:https://blog.csdn.net/VRunSoftYanlz/article/details/88784527
++++SteamVR2.2.0快速入门:https://blog.csdn.net/VRunSoftYanlz/article/details/88833579
++++SteamVR2.2.0交互系统:https://blog.csdn.net/VRunSoftYanlz/article/details/89199778
++++SteamVR2.2.0传送机制:https://blog.csdn.net/VRunSoftYanlz/article/details/89390866
++++SteamVR2.2.0教程(一):https://blog.csdn.net/VRunSoftYanlz/article/details/89324067
++++SteamVR2.2.0教程(二):https://blog.csdn.net/VRunSoftYanlz/article/details/89894097
++++SteamVR_Skeleton_Poser:https://blog.csdn.net/VRunSoftYanlz/article/details/89931725
++++SteamVR实战之PMCore:https://blog.csdn.net/VRunSoftYanlz/article/details/89463658
++++SteamVR/Extras:https://blog.csdn.net/VRunSoftYanlz/article/details/86584108
++++SteamVR/Input:https://blog.csdn.net/VRunSoftYanlz/article/details/86601950
++++OpenXR简介:https://blog.csdn.net/VRunSoftYanlz/article/details/85726365
++++VRTK杂谈:https://blog.csdn.net/VRunSoftYanlz/article/details/82562993
++++VRTK快速入门(杂谈):https://blog.csdn.net/VRunSoftYanlz/article/details/82955267
++++VRTK官方示例(目录):https://blog.csdn.net/VRunSoftYanlz/article/details/82955410
++++VRTK代码结构(目录):https://blog.csdn.net/VRunSoftYanlz/article/details/82780085
++++VRTK(SceneResources):https://blog.csdn.net/VRunSoftYanlz/article/details/82795400
++++VRTK_ControllerEvents:https://blog.csdn.net/VRunSoftYanlz/article/details/83099512
++++VRTK_InteractTouch:https://blog.csdn.net/VRunSoftYanlz/article/details/83120220
++++虚拟现实行业应用:https://blog.csdn.net/VRunSoftYanlz/article/details/88360157
++++Steam平台上的VR:https://blog.csdn.net/VRunSoftYanlz/article/details/88960085
++++Steam平台热销VR:https://blog.csdn.net/VRunSoftYanlz/article/details/89007741
++++VR实验:以太网帧的构成:https://blog.csdn.net/VRunSoftYanlz/article/details/82598140
++++实验四:存储器扩展实验:https://blog.csdn.net/VRunSoftYanlz/article/details/87834434
++++FrameVR示例V0913:https://blog.csdn.net/VRunSoftYanlz/article/details/82808498
++++FrameVR示例V1003:https://blog.csdn.net/VRunSoftYanlz/article/details/83066516
++++SwitchMachineV1022:https://blog.csdn.net/VRunSoftYanlz/article/details/83280886
++++PlaySceneManagerV1022:https://blog.csdn.net/VRunSoftYanlz/article/details/83280886
++++Unity5.x用户手册:https://blog.csdn.net/VRunSoftYanlz/article/details/81712741
++++Unity面试题ABC:https://blog.csdn.net/vrunsoftyanlz/article/details/78630687
++++Unity面试题D:https://blog.csdn.net/VRunSoftYanlz/article/details/78630838
++++Unity面试题E:https://blog.csdn.net/vrunsoftyanlz/article/details/78630913
++++Unity面试题F:https://blog.csdn.net/VRunSoftYanlz/article/details/78630945
++++Cocos2dx面试题:https://blog.csdn.net/VRunSoftYanlz/article/details/78630967
++++禅道[zentao]:https://blog.csdn.net/VRunSoftYanlz/article/details/83964057
++++Lua快速入门篇(Xlua拓展):https://blog.csdn.net/VRunSoftYanlz/article/details/81173818
++++Lua快速入门篇(XLua教程):https://blog.csdn.net/VRunSoftYanlz/article/details/81141502
++++Lua快速入门篇(基础概述):https://blog.csdn.net/VRunSoftYanlz/article/details/81041359
++++框架知识点:https://blog.csdn.net/VRunSoftYanlz/article/details/80862879
++++游戏框架(UI框架夯实篇):https://blog.csdn.net/vrunsoftyanlz/article/details/80781140
++++游戏框架(初探篇):https://blog.csdn.net/VRunSoftYanlz/article/details/80630325
++++.Net框架设计:https://blog.csdn.net/VRunSoftYanlz/article/details/87401225
++++从零开始学架构:https://blog.csdn.net/VRunSoftYanlz/article/details/88095895
++++设计模式简单整理:https://blog.csdn.net/vrunsoftyanlz/article/details/79839641
++++专题:设计模式(精华篇):https://blog.csdn.net/VRunSoftYanlz/article/details/81322678
++++U3D小项目参考:https://blog.csdn.net/vrunsoftyanlz/article/details/80141811
++++Unity小游戏算法分析:https://blog.csdn.net/VRunSoftYanlz/article/details/87908365
++++Unity案例(Vehicle):https://blog.csdn.net/VRunSoftYanlz/article/details/82355876
++++UML类图:https://blog.csdn.net/vrunsoftyanlz/article/details/80289461
++++PowerDesigner简介:https://blog.csdn.net/VRunSoftYanlz/article/details/86500084
++++Unity知识点0001:https://blog.csdn.net/vrunsoftyanlz/article/details/80302012
++++Unity知识点0008:https://blog.csdn.net/VRunSoftYanlz/article/details/81153606
++++U3D_Shader编程(第一篇:快速入门篇):https://blog.csdn.net/vrunsoftyanlz/article/details/80372071
++++U3D_Shader编程(第二篇:基础夯实篇):https://blog.csdn.net/vrunsoftyanlz/article/details/80372628
++++Unity引擎基础:https://blog.csdn.net/vrunsoftyanlz/article/details/78881685
++++Unity面向组件开发:https://blog.csdn.net/vrunsoftyanlz/article/details/78881752
++++Unity物理系统:https://blog.csdn.net/vrunsoftyanlz/article/details/78881879
++++Unity2D平台开发:https://blog.csdn.net/vrunsoftyanlz/article/details/78882034
++++UGUI基础:https://blog.csdn.net/vrunsoftyanlz/article/details/78884693
++++UGUI进阶:https://blog.csdn.net/vrunsoftyanlz/article/details/78884882
++++UGUI综合:https://blog.csdn.net/vrunsoftyanlz/article/details/78885013
++++Unity动画系统基础:https://blog.csdn.net/vrunsoftyanlz/article/details/78886068
++++Unity动画系统进阶:https://blog.csdn.net/vrunsoftyanlz/article/details/78886198
++++Navigation导航系统:https://blog.csdn.net/vrunsoftyanlz/article/details/78886281
++++Unity特效渲染:https://blog.csdn.net/vrunsoftyanlz/article/details/78886403
++++Unity数据存储:https://blog.csdn.net/vrunsoftyanlz/article/details/79251273
++++Unity中Sqlite数据库:https://blog.csdn.net/vrunsoftyanlz/article/details/79254162
++++WWW类和协程:https://blog.csdn.net/vrunsoftyanlz/article/details/79254559
++++Unity网络:https://blog.csdn.net/vrunsoftyanlz/article/details/79254902
++++Unity资源加密:https://blog.csdn.net/VRunSoftYanlz/article/details/87644514
++++PhotonServer简介:https://blog.csdn.net/VRunSoftYanlz/article/details/86652770
++++编写Photon游戏服务器:https://blog.csdn.net/VRunSoftYanlz/article/details/86682935
++++C#事件:https://blog.csdn.net/vrunsoftyanlz/article/details/78631267
++++C#委托:https://blog.csdn.net/vrunsoftyanlz/article/details/78631183
++++C#集合:https://blog.csdn.net/vrunsoftyanlz/article/details/78631175
++++C#泛型:https://blog.csdn.net/vrunsoftyanlz/article/details/78631141
++++C#接口:https://blog.csdn.net/vrunsoftyanlz/article/details/78631122
++++C#静态类:https://blog.csdn.net/vrunsoftyanlz/article/details/78630979
++++C#中System.String类:https://blog.csdn.net/vrunsoftyanlz/article/details/78630945
++++C#数据类型:https://blog.csdn.net/vrunsoftyanlz/article/details/78630913
++++Unity3D默认的快捷键:https://blog.csdn.net/vrunsoftyanlz/article/details/78630838
++++游戏相关缩写:https://blog.csdn.net/vrunsoftyanlz/article/details/78630687
++++UnityAPI.Rigidbody刚体:https://blog.csdn.net/VRunSoftYanlz/article/details/81784053
++++UnityAPI.Material材质:https://blog.csdn.net/VRunSoftYanlz/article/details/81814303
++++UnityAPI.Android安卓:https://blog.csdn.net/VRunSoftYanlz/article/details/81843193
++++UnityAPI.AndroidJNI安卓JNI:https://blog.csdn.net/VRunSoftYanlz/article/details/81879345
++++UnityAPI.Transform变换:https://blog.csdn.net/VRunSoftYanlz/article/details/81916293
++++UnityAPI.WheelCollider轮碰撞器:https://blog.csdn.net/VRunSoftYanlz/article/details/82356217
++++UnityAPI.Resources资源:https://blog.csdn.net/VRunSoftYanlz/article/details/83155518
++++JSON数据结构:https://blog.csdn.net/VRunSoftYanlz/article/details/82026644
++++CocosStudio快速入门:https://blog.csdn.net/VRunSoftYanlz/article/details/82356839
++++Unity企业内训(目录):https://blog.csdn.net/VRunSoftYanlz/article/details/82634668
++++Unity企业内训(第1讲):https://blog.csdn.net/VRunSoftYanlz/article/details/82634733
++++Unity企业内训(第2讲):https://blog.csdn.net/VRunSoftYanlz/article/details/82861180
++++Unity企业内训(第3讲):https://blog.csdn.net/VRunSoftYanlz/article/details/82927699
++++Unity企业内训(第4讲):https://blog.csdn.net/VRunSoftYanlz/article/details/83479776
++++Unity企业内训(第5讲):https://blog.csdn.net/VRunSoftYanlz/article/details/83963811
++++Unity企业内训(第6讲):https://blog.csdn.net/VRunSoftYanlz/article/details/84207696
++++钻哥带您了解产品原型:https://blog.csdn.net/VRunSoftYanlz/article/details/87303828
++++插件
++++计算机组成原理(教材篇):https://blog.csdn.net/VRunSoftYanlz/article/details/82719129
++++5G接入:云计算和雾计算:https://blog.csdn.net/VRunSoftYanlz/article/details/88372718
++++云计算通俗讲义:https://blog.csdn.net/VRunSoftYanlz/article/details/88652803
++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz
--_--VRunSoft:lovezuanzuan--_--