unity3d 学习笔记_____Native2d 刚体、冲击、联合使用
2015-08-19 19:56
375 查看
Mass | Mass of the rigidbody. |
Linear Drag | Drag coefficient affecting positional movement. |
Angular Drag | Drag coefficient affecting rotational movement. |
Gravity Scale | Degree to which the object is affected by gravity. |
Fixed Angle | Can the rigidbody rotate when forces are applied? |
Is Kinematic | Is the rigidbody moved by forces and collisions? |
Interpolate | How the object's movement is interpolated between physics updates (useful when motion tends to be jerky). |
None | No movement smoothing is applied. |
Interpolate | Movement is smoothed based on the object's positions in previous frames. |
Extrapolate | Movement is smoothed based on an estimate of its position in the next frame. |
Sleeping Mode | How the object "sleeps" to save processor time when it is at rest. |
Never Sleep | Sleeping is disabled. |
Start Awake | Object is initially awake. |
Start Asleep | Object is initially asleep but can be woken by collisions. |
Collision Detection | How collisions with other objects are detected. |
Discrete | A collision is registered only if the object's collider is in contact with another during a physics update. |
Continuous | A collision is registered if the object's collider appears to have contacted another between updates. |
(1)刚体碰撞
(2) 触发碰撞、会穿透其它刚体
相应Collider组件中的is Trigger,两种都会产生碰撞事件
void OnCollisionEnter2D(Collision2D cod) { print (cod.gameObject.name); if(cod.rigidbody) cod.rigidbody.AddForce(new Vector2(0,500f)); }
void OnTriggerEnter2D(Collider2D other) { Destroy(other.gameObject); }
两个方法都属于MonoBehaviour的Message 回调方法,注意区分它们的參数类型是不同的
另外还有 OnTriggerExit2D、OnTriggerStay2D、 OnCollisionExit2D、OnCollisionStay2D
关节的使用:
SpringJoint和DistantanceJoint有点类似多了弹性參数和频率设置(在unity中临时没看出效果)
hingeJoint 能够理解为一个环绕Z轴旋转的关节,能够设置响应moto、以及角度的限制
sliderJoint 滑动关节类似hingeJoint 以一个角度值设置Moto进行滑动,能够对距离(translation)进行限制
以下是一个用HingeJoint做的一个demo:
两仅仅小鸟同一时候加入HingeJoint 连接到盒子。后面的小鸟加下下面脚本控制方向键盘就可以前后运动
using UnityEngine; using System.Collections; [RequireComponent(typeof(HingeJoint2D))] public class MotoControl : MonoBehaviour { public float MotoSpeed = 0; private JointMotor2D motor; HingeJoint2D hj; // Use this for initialization void Start () { hj = GetComponent<HingeJoint2D>(); motor = hj.motor; hj.useMotor = true; motor = hj.motor; motor.motorSpeed = MotoSpeed; motor.maxMotorTorque = 10000; } // Update is called once per frame void Update () { motor.motorSpeed = Input.GetAxis("Horizontal") * MotoSpeed; hj.motor = motor; } }
做Demo时遇到的问题:
(1)刚開始用hj.motor.motorSpeed一直报错,后来分两步写最终没问题
motor = hj.motor; motor.motorSpeed = MotoSpeed;
(2)错是没了,但是小车还是不走,最后知道还须要把motor对象又一次赋给HingeJoint
motor.motorSpeed = Input.GetAxis("Horizontal") * MotoSpeed; hj.motor = motor;
其它一些经常使用的属性:
breakForce、breakTorque分别设定多大力、多大扭矩能给丫的拆了
connectedBody 连接的另外一个刚体的引用 (Joint2D中不存在)
hj.connectedBody = null;连接到一个空对象上
要想断开关节直接去掉关节组件 destroy(hj);
断开将发送 OnJointBreak Message.
相关文章推荐
- 全面理解Unity加载和内存管理
- Unity 3D中的内存管理
- Unity3d基础组件 (Component) 和脚本 (Script) 关系
- Unity项目中UI同学需知的程序相关要点
- Unity Shader 学习笔记 (八) 语义词与语义绑定
- 浅尝Unity 3D的Asset Bundle知识(七)-----依赖的利用
- Unity 3D Android对dll加密和重编译mono源码进行解密
- unity3d 系统方法执行顺序
- Unity 2D 教程汇总
- Unity 2D 英文教程(www.raywenderlich.com)
- Unity 5.x的专用插件推荐
- 全面理解 Unity UI 系统
- Unity3D游戏开发之游戏读/存档功能在Unity3D中的实现
- Unity3D性能优化之角色建模
- Unity 2D教程: 滚动,场景和音效
- Unity 2D入门基础教程
- unity使用抽象工厂模式
- Unity中做放大镜 效果
- Unity Asset(4)特性插件 FX Maker
- Unity Asset(3)虚拟摇杆插件 EasyTouch