🤑 Indie game store🙌 Free games😂 Fun games😨 Horror games
👷 Game development🎨 Assets📚 Comics
🎉 Sales🎁 Bundles

[help] Getting an actor involved in collision

A topic by darkdrewmo created 1 year ago Views: 280 Replies: 1
Viewing posts 1 to 2

What I am trying to accomplish is if an my object collides with something I would like to check and see what the collision is, then if it is a specific actor I want to check what animation is playing on that actor. I am unsure how to get the actor from the collision. see example below for what I have right now.

if (Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.getActor("collision").arcadeBody2D) { 
   if (Sup.getActor("collision").spriteRenderer.getAnimation() == "animation1") { 
      //do something... 
   } 
}

After this step I am going to make a generic function so I can do whatever I want without needing to put this code everywhere. Something like:

collidesWith(target, animation) { 
   if (target.getAnimation() == animation) { 
      //do something.. 
   } 
}

My current issue is I don't know the proper way to get the actor that was involved in the collision. For example if a car collides with a wall how can I reference that wall's actor with a collision script inside of the car?

I had posted this on Reddit too, I will update either posting with the working response.

I guess you should to make something like this:

//{Wall Script}

//Verify the collision
let collide = Sup.ArcadePhysics2D.collides(
           Sup.getActor('Car').arcadeBody2D, Sup.getActor('Wall').arcadeBody2D);

if (collide) {
console.log('Bateu!');
//Call Animation Sup.getActor('Car').spriteRenderer.setAnimation('Collision');
} else {
console.log('Driver Safe!');
}

I put my collision's scripts in the static actors (e.g. wall) when they start the event (collision), I just pick the actor in the scene that "hit" the my static actor and animate. Hope it helps :)