能检测到碰撞的物体都需要
碰撞事件,需要有碰撞器,至少有一方是有刚体的,最好是运动的一方
防止抖动方法,FixedUpdate
两个碰撞体
box 2d
private void fixedupdate(){}
碰撞检测的两个必要条件
1、组件身上要有碰撞器
2、其中一方要有刚体(最好是运动的一方)。
原因:刚体长时间不运动会休眠就不会发生碰撞检测。
每一帧受力是相同的,不能发生变化。所以放到物理帧中:FixedUpdate();
碰撞器的生成
二者都要有碰撞器
其中一方(运动的一方)要有缸体
fixedupdate 物理针
是每一秒受力固定
缸体碰撞受力使物体发生抖动,添加物理针处理
未加物理针
void Update ()
{
float h = Input.GetAxisRaw("Horizontal");
transform.Translate(Vector3.right * h * movespeed * Time.deltaTime, Space.World);
if (h < 0)
{
sr.sprite = tanksprite[3];
}
else if (h > 0)
{
sr.sprite = tanksprite[1];
}
float v = Input.GetAxisRaw("Vertical");
transform.Translate(Vector3.up * v * movespeed * Time.deltaTime, Space.World);
if (v < 0)
{
sr.sprite = tanksprite[2];
}
else if (v > 0)
{
sr.sprite = tanksprite[0];
}
}
添加物理针
private void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
transform.Translate(Vector3.right * h * movespeed * Time.fixedDeltaTime, Space.World);
if (h < 0)
{
sr.sprite = tanksprite[3];
}
else if (h > 0)
{
sr.sprite = tanksprite[1];
}
float v = Input.GetAxisRaw("Vertical");
transform.Translate(Vector3.up * v * movespeed * Time.fixedDeltaTime, Space.World);
if (v < 0)
{
sr.sprite = tanksprite[2];
}
else if (v > 0)
{
sr.sprite = tanksprite[0];
}
}