当前位置:网站首页>Unity学习笔记(刚体-物理-碰撞器-触发器)
Unity学习笔记(刚体-物理-碰撞器-触发器)
2022-07-27 16:22:00 【Miracle Fan】
1.Rigidbody-刚体

1.Property
| Property: | Function: |
|---|---|
| Mass | 物体质量,默认质量(单位) |
| Drag | 物体运动受到的空气阻力 |
| Angular Drag | 物体旋转时受到的空气阻尼引起的转矩 |
| Use Gravity | 是否受到重力影响 |
| Is Kinematic | 如果符合动力学,则物体不受物理引擎作用,只能通过调整改变,但仍能与其他刚体作用,一般用于测试。 |
[SerializeField] private float _forceAmount = 100f;
//[SerializeField]为序列化字段,可以将私有字段显示在unity引擎进行调试
private Rigidbody _rigidbody;
// Start is called before the first frame update
void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))//点击一次鼠标左键执行一次
{
_rigidbody.AddForce(Vector3.up * _forceAmount);
}
}
2.物理材质

1.Property:
| Property: | Function: |
|---|---|
| Dynamic Friction | 动摩擦力,数值通常在0~1之间,为0则表示光滑平面 |
| Static Friction | 静摩擦力,通常在0~1之间 |
| Bounciness | 弹性指数,0~1,0则没有弹力,1为完全弹性,不损失能量 |
| Friction Combine | 当两个碰撞体发生碰撞如何计算摩擦力 |
| - Average | 选取两者平均值 |
| - Minimum | 选取两者最小值 |
| - Maximum | 选取两者最大值 |
| - Multiply | 选取两者相乘 |
| Bounce Combine | 等同于Friction Combine |
2.创建过程

3. Collision

3.1 Edit Collider
可以修改碰撞体大小,只要接触碰撞体设定范围,就会触发OncollisionEnter()函数

OncollisionEnter()函数为当物体发生碰撞后激发的函数。Material为碰撞体物理材质,需要自己进行创建添加。
private Renderer _renderer;
void Awake()
{
//获得组件渲染器对象
_renderer = GetComponent<Renderer>();
}
private void OnCollisionEnter(Collision collision)
{
Color randomlySelectedColor=GetRandomColor();
_renderer.material.color=randomlySelectedColor;
}
private Color GetRandomColor()
{
return new Color(
r:UnityEngine.Random.Range(0f,1f),
g:UnityEngine.Random.Range(0f,1f),
b:UnityEngine.Random.Range(0f,1f));
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("Impacted at" +collision.contacts[0].point);
float rayDrawDistance = 5f;
Debug.DrawRay(
collision.contacts[0].point,
collision.contacts[0].normal*rayDrawDistance,//接触点法线方向
Color.red,
1f);
}
4. Trigger
OnTriggerEnter()函数为当物体触发到Trigger物体后激发函数。
void Awake()
{
//获得组件渲染器对象
_renderer = GetComponent<Renderer>();
}
private void OnTriggerEnter(Collider other)
{
Color randomlySelectedColor = GetRandomColorWithAlpha();
_renderer.material.color = randomlySelectedColor;
}
private Color GetRandomColorWithAlpha()
{
return new Color(
r: UnityEngine.Random.Range(0f, 1f),
g: UnityEngine.Random.Range(0f, 1f),
b: UnityEngine.Random.Range(0f, 1f),
a:0.25f);
}
下图为设置Trigger后的变化情况,Trigger物体不影响其他物体的运动。


边栏推荐
- CMD 命令
- Filebeat.yml configuration file about the configuration of multiple services
- Redis注解
- 多功能无线遥控艾灸仪芯片-DLTAP703SD
- Uni app traversal array rendering data vertical rendering
- C basic concepts list description suggestions collection
- Nodejs 模板引擎ejs
- JDBC-MySql 02 数据访问和DAO模式
- MySQL 04 高级查询(二)
- Day 3 of leetcode question brushing
猜你喜欢
随机推荐
was not registered for synchronization because synchronization is not active[已解决]
Latex使用--subfigure竖排图形
Functions in JS and the use of DOM to obtain elements and event attributes
浴室带除雾化妆镜触摸芯片-DLT8T10S
Typescript installation
内网的公司邮箱服务器怎么发外部邮件
订单的提交
js中的函数与DOM获取元素和事件属性的使用
npm的身份证和依赖
LeetCode 刷题 第二天
商品评论信息与评论信息分类
Uni app traversal array rendering data vertical rendering
Here are all the MySQL interview questions you can't expect (the latest version of 2022)
Extension of ES6 value
Interceptor interceptor
C static method and non static method
JS to realize simple form verification and select all functions
MySQL 03 advanced query (I)
Aircraft battle with enemy aircraft
商品推荐和分类商品推荐









