OnCollision check without Enter
I am creating random prefabs at random places. But sometimes, they are being created on something important. Like tree is being created on river.
And I want to destroy everything on river at start. I am using this code:
void OnCollisionEnter(Collision col)
{
Destroy(col.gameObject);
}
But as you may guess, it's not working. Because the prefabs are being created at start without a force. They just pop. And because of this, it does not trigger OnCollision*Enter*
Is there a OnCollision check for any situation? Enter, exit etc...
Answer by PMasta · Jan 03, 2016 at 11:02 AM
It looks like Unity doesent check collisions with not moving objects even if they have a collider. I fixed this issue like this:
1 - Add mesh collider (or box, it's up to your object) to both spawned objects and area (it's river in my case).
2 - Add Destroy script to the area (river):
void OnCollisionEnter(Collision col)
{
Destroy(col.gameObject);
}
3 - Add RigidbodyManager script to the spawned objects:
public Rigidbody rb;
void Start()
{
// Get's objects rigidboy.
rb = GetComponent<Rigidbody>();
// Set's it to kinematic so it won't move.
rb.isKinematic = true;
// Destroys rigidbody so it won't make your processor run thousands of spawned objects rigidbodies.
Destroy(rb);
}
4 - Add Rigidbody to spawned objects (prefabs if you are going to spawn from prefab). Don't check isKinematic. Just leave it as its created.
5 - Profit?
Answer by itay3x3 · Jan 02, 2016 at 06:50 PM
Try something like this:
if (transform.position.x > river.transform.position.x - river.transform.localScale.x / 2 && transform.position.x < river.transform.position.x + river.transform.localScale.x / 2 && transform.position.z > river.transform.position.z - river.transform.localScale.z / 2 && transform.position.z < river.transform.position.z + river.transform.localScale.z / 2) {
Destroy(gameobject);
}
Your answer
Follow this Question
Related Questions
CapsuleCollider check if grounded 1 Answer
How to Make A Character Stop At Wall? 0 Answers
problem with tilemap collider and composite collider 1 Answer
Animator and collision issues 0 Answers
Adding different values to randomly created objects 0 Answers