- Home /
OnCollisionEnter2D question
Hello every one ..
private void OnCollisionEnter2D(Collision2D col)
{
if (col.relativeVelocity.magnitude > 5)
Instantiate(FallDust, transform.position, Quaternion.identity);
}
i put this script in a multiple cars that keep falling from the sky and the code job is to create dust when its hit anything if the speed was greater than 5 and its works fine ,But even if the car was not moving and other falling cars with speed>5 hit it both cars will create dust the one with speed=0 and the other one with speed >5
I mean possibly the transfer of momentum to the still car, increases its relative velocity? Why not use the rigidbody.velocity?
You double-checked that car's speed is 0 when it instantiate the particles?
In every place you instantiate the particles, do print(col.relativeVelocity.magnitude);
to see the speed in that very moment on the console and make sure is as expected.
Answer by logicandchaos · Jan 12, 2021 at 11:54 AM
You have if the collided object has magnitude >5 then the object the script is on gets the dust, change Instantiate(FallDust, transform.position, Quaternion.identity); to Instantiate(FallDust, col.transform.position, Quaternion.identity); to get the transform of the collided object instead.
i have tried it ,it didn't work its create dust to still object even the ones that didn't has the script.
Let me explain my problem again: the car fall from the sky hit the ground the dust created . another car hit the car that on the ground both cars create dust, And this is the problem i want only the one falling to create dust .
Your answer
Follow this Question
Related Questions
OnCollisionEnter If statement error 1 Answer
Checking for collision of another gameObject with another trigger 1 Answer
Cannot get collision to do anything 1 Answer
OnCollisionEnter not working 2 Answers
get collision info from main script? 1 Answer