2016-03-26 27 views
0

我正在製作一個彈丸,我想要摧毀敵方物體,這將是基本浮動物體
如何獲取它,以便兩個物體在被特定演員擊中時銷燬。ue4 C++ destory on hit如果指定者演員

void APrimaryGun::NotifyHit(UPrimitiveComponent* MyComp, AActor* Other, UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) 
{ 
    if (GEngine) 
    { 
     GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Red, "Hit"); 
    } 

    Destroy(); 

    Other->Destroy(); 
} 

上面是我目前的情況,但是破壞了它所擊中的任何東西,這不是我想要的。

我相信它應該是一個簡單的if語句,但不確定如何寫它。
在此先感謝

回答

0

其他是任何AActor,所以每次你擊中一個AActor你就會摧毀彈丸和其他。我假設你想要發生的是,如果你的子彈擊中任何東西,子彈就會被銷燬,如果它擊中的對象是正確的類型,那麼這個對象也會被銷燬。

假設你想被炮彈擊毀的物體是由AActor派生的。喜歡的東西:

class DestroyableEnemy : public AActor 
    { //class definition 

    }; 

所以,你知道你的其他是一個指向AActor,你想知道什麼呢,如果,具體地講,它是一個指向DestroyableEnemy(或任何你把它命名)。你可以用C++做的兩種方法是dynamic_cast和typeid運算符。我知道如何執行它的方式是使用dynamic_cast。你會嘗試並將通用的AActor投入到DestroyableEnemy。如果它是一個可銷燬的敵人,你會得到一個指向它的指針。如果不是,你只需要一個空指針。 https://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI

+0

感謝這正是我想要的,你知道我怎麼會在C實施破壞的網狀++而不是僅僅從級別中取出任何機會:

DestroyableEnemy* otherEnemy = dynamic_cast<DestroyableEnemy*>(Other); if(otherEnemy){ //if otherEnemy isn't null, the cast succeeded because Other was a destroyableEnemy, and you go to this branch otherEnemy->Destroy(); }else{ // otherEnemy was null because it was some other type of AActor Other->SomethingElse(); //maybe add a bullet hole? Or nothing at all is fine }; 

摘自? –

+0

喜歡它爆炸,例如?這更像是一個動畫事物,並且您希望動畫在發生事件(如hp = 0)時觸發。 –

+0

一位朋友設法幫助我實現它,謝謝你的所有幫助,儘管 –