2017-06-15 103 views
0

我正處於一個遊戲工作的中間,而我剛剛開始。我在地圖上添加了png,並且在我想要無法通過的區域周圍添加了對撞機(它是2D平臺遊戲)。我有一個敵人已經設計並增加了2DRigidBody組件,因爲它四處移動,並開始使用2DBoxColliders作爲對撞機的水平,我的劇本我寫了:二維多邊形對撞機不會導致與二維多邊形對撞機碰撞

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class Sexapus : MonoBehaviour { 

public static int Velocity = 42; 
public Rigidbody2D rb; 
public Vector2 dir; 
public Animator anim; 

void Start() { 
    rb = GetComponent<Rigidbody2D>(); 
    dir = new Vector2(1, 0); 
    anim = GetComponent<Animator>(); 
} 

// Update is called once per frame 
void Update() { 
    rb.velocity = dir * Velocity; 
    anim.SetFloat ("Direction", dir.x); 
} 

void OnCollisionEnter2D (Collision2D col) { 
    dir = dir * -1; 
} 

//CHECK TASKS 
} 

意味着敵人會撞到牆的一側,然後旋轉並開始轉向另一個方向。我意識到我的地圖大小使用多個2DBoxColliders(當我說多個時,我的意思是我可能需要使用一百多個),這是一個非常糟糕的方式。我現在已經開始爲地圖使用2DpolygonCollider,但現在敵人不會與牆壁碰撞並轉身,它只會朝向相同的方向,但不會移動。有人知道爲什麼

回答

0

您正在爲地板和牆壁使用相同的對撞機......因此,當您碰撞牆壁並且已經接觸地板時,OnCollisionEnter不會發生。

所以,你確實得到了碰撞,因此物體不能移過牆壁,但它不是新的碰撞!

解決方案:在地板上使用對撞機,在牆上使用不同的碰撞機,邊緣碰撞機對此很有幫助。