您的位置:首页 > 移动开发 > Unity3D

unity3d 学习笔记_____Native2d 刚体、冲击、联合使用

2015-08-19 19:56 375 查看




MassMass of the rigidbody.
Linear DragDrag coefficient affecting positional movement.
Angular DragDrag coefficient affecting rotational movement.
Gravity ScaleDegree to which the object is affected by gravity.
Fixed AngleCan the rigidbody rotate when forces are applied?

Is KinematicIs the rigidbody moved by forces and collisions?
InterpolateHow the object's movement is interpolated between physics updates (useful when motion tends to be jerky).
NoneNo movement smoothing is applied.
InterpolateMovement is smoothed based on the object's positions in previous frames.
ExtrapolateMovement is smoothed based on an estimate of its position in the next frame.
Sleeping ModeHow the object "sleeps" to save processor time when it is at rest.
Never SleepSleeping is disabled.
Start AwakeObject is initially awake.
Start AsleepObject is initially asleep but can be woken by collisions.
Collision DetectionHow collisions with other objects are detected.
DiscreteA collision is registered only if the object's collider is in contact with another during a physics update.
ContinuousA collision is registered if the object's collider appears to have contacted another between updates.
碰撞器Collider 分为两种:

(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 回调方法,注意区分它们的參数类型是不同的

另外还有 OnTriggerExit2DOnTriggerStay2DOnCollisionExit2DOnCollisionStay2D

关节的使用:



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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: