,顯示了碰撞不一致GIF格式:http://imgur.com/QB4XOv0.gifOnTriggerEnter2D不叫一貫
在我Unity2D比賽,我有一個彈弓(玩家)發射彈珠了一堵牆(認爲Puzzle Bobble型益智射擊遊戲)。每個彈珠是克隆預製的每個預製有剛體與IsKinematic與OnTrigger檢查與CircleCollider一起檢查。
當大理石與兩個側壁碰撞時,我使用OnTriggerEnter2D()並旋轉角度使其以一定角度從牆壁反彈。當大理石與頂壁發生碰撞時,它基本上會凍結,此時我將零部件去除,並設置一個標記「01212」。
當大理石與另一塊大理石碰撞時會出現不一致。有時會調用觸發器函數,有時則不會。觸發器似乎是隨機的和不可預知的。
這裏是(附着在大理石),用於檢測碰撞,並與它一起去的行爲,我的C#代碼:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RockMove : MonoBehaviour
{
public float speed = 0.5f;
private Vector2 direction = new Vector2(0, 1);
// current angle
private float currentAngleZ;
// indendiary angle
private float angleB;
// rotation speed
private float rot;
// curving speed
private float rotMod;
// stops marble when collides with wall
private bool IsMoving = true;
void Start() {
}
void Update()
{
if (IsMoving) {
rot = RockSpawn.rot;
currentAngleZ = transform.eulerAngles.z;
rotMod = transform.position.x * -0.5f;
// moves it forward + adds speed
transform.Translate(direction * Time.deltaTime * speed, Space.Self);
// curving
transform.Rotate (0, 0, rotMod, Space.Self);
if (transform.position.x >= 0) {
// faster speed when near walls "gravity/magnetism"
speed += transform.position.x * 8 * Time.deltaTime;
}
if (transform.position.x < 0) {
// faster speed when near walls "gravity/magnetism"
speed += transform.position.x * -8 * Time.deltaTime;
}
}
else {
// when object is stationary
speed = 0;
}
}
void OnTriggerEnter2D(Collider2D collider){
// if collides with another marble
if (collider.gameObject.tag == "topObjects") {
// get rid of rigidbody
// can't have 2 rigidbodies when 2 objects collide
Destroy(rigidbody2D);
// freeze movement
IsMoving = false;
Debug.Log ("you collided with a marble");
// freeze rotation
this.transform.Rotate (0, 0, 0, Space.Self);
// assign the new tag
this.tag = "topObjects";
}
else if(collider.name == "TopWall")
{
Destroy(rigidbody2D);
IsMoving = false;
Debug.Log ("you broke the fourth wall!");
transform.Rotate (0, 0, 0);
this.tag = "topObjects";
}
else {
// bounce off walls
currentAngleZ = transform.eulerAngles.z;
angleB = 360 - (2 * currentAngleZ);
transform.Rotate (0, 0, angleB);
}
}
}
更新:簡單地改變更新()函數來FixedUpdate()似乎使觸發器更加一致,但是它們如何被檢測到仍然存在不一致。
我測試了rigidbody2d.velocity,transform.position和其他各種變換函數,但是Transform.Translate產生了最好的效果,即使它理論上只是傳送對象到一個新的位置。
降低速度也不會影響一致性,也不會改變圓形碰撞體的大小。
以我的經驗,碰撞和觸發事件在使用基於物理的運動時更加一致,例如'rigidbody.velocity'。 – SirBraneDamuj 2014-09-30 19:12:34
我認爲問題與你的牆壁有關。當氣泡在牆上反彈時,您的gif只顯示碰撞不一致。你的牆被設置爲觸發器還是隻是常規的對撞機? – SirBraneDamuj 2014-10-01 13:41:02
牆壁也設置爲觸發器(選中與IsTrigger相對的多邊形對撞機) – BankBits 2014-10-01 13:44:08