- Home /
Which one of the objects should report back the collision?
Two objects hit each other. On hit one of the objects should call the function CollisionHappened(GameObject objectA, GameObject objectB) in ListHolderCollisions and then that pair should be added to a list. How would you guys go about calling the CollisionHappened only once even though both objects have the same functionality.
ListHolderCollisions will be a Singleton
public class UnitScript: MonoBehaviour
{
public void OnCollisionEnter(Collision collision)
{
//call CollisionHappened
}
}
public class ListHolderCollisions: MonoBehaviour
{
List<UnitCollisionClass> listOfCollsions = new List<UnitCollisionClass>();
public void CollisionHappened(GameObject objectA, GameObject objectB)
{
listOfCollsions.Add(new UnitCollisionClass(objectA, objectB));
}
}
public class UnitCollisionClass
{
public GameObject objectA;
public GameObject objectB;
public UnitCollisionClass(GameObject objectA, GameObject objectB)
{
this.objectA = objectA;
this.objectB = objectB;
}
}
Answer by xxmariofer · Aug 27, 2020 at 12:13 PM
maybe not the most efficient solution but an easy to implement solution without modifyng mucho your code would be:
public class UnitScript: MonoBehaviour
{
public bool isColliding { get; set; }
public void OnCollisionEnter(Collision collision)
{
if(isColliding) return;
collision.gameObject.GetComponent<UnitScript>().isColliding = true;
ListHolderCollisions.singleton.listCollisions.Add(new UnitCollisionClass(gameObject, collision.gameObject));
}
}
you will have to set isColliding back to false whenever you want to detect collisions again
Yhea that's something that came to my $$anonymous$$d while writing this question. A bool would be ok i guess. But what about other ways to implement such a thing? Could you point me to some good documentation. I am curios of how people would implement such feature in a professional environment, do they just use a bool or is there a smarter way to that?
i would need the exact gameplay mechanic, there are multiple options, an alternative is to send the gameobjects both to times to the ListHolderCollisions instance and let the class handle which gamobjects are already in the list
There can be up to on hundred objects in the scene. Collisions between them can happen randomly. $$anonymous$$ultiple collisions can happen at the same time, although it is unlikely. Each collision must be registered by being added to the list found in ListHolderCollisions.
And that is basically it. The reason why i did not went with the list thing that you mentioned is because i did not know how the function would behave if two collisions would happen at the same time. $$anonymous$$aybe then instead of having list[0] and list[1] as the right entries i would have list[0] and list[2] (or something like that) because the another collision would be called and fill up list[1] and list[3]. Or correct me if i am wrong in regards to that.
PS: sorry for not mentioning the multiple collisions happening at the same time. And thanks for the help!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Callback method to a destroyed object 2 Answers
set active object using c# 1 Answer
I need help writing a simple script for making an object face the mouse in 2D. 2 Answers