- Home /
How to get transform of ContactPoint in OnCollisionEnter?
I try to make two objects cause sparks when they collide, I think I should use ContactPoint. The problem is I have no idea how to get the transform of the exact spot of collision. Is it even possible? Or should I use different methode? Here is my uncomplete script:
public Object Sparks;
public Transform Location;
void OnCollisionEnter (Collision col) {
Location = col.contacts?; //Get the transform of contact point [HELP]
Object Effects;
Effects = Instantiate(Sparks, Location.position, Location.rotation);
}
Answer by robertbu · Sep 21, 2014 at 07:35 AM
Location = col.contacts[0].otherCollider.transform;
When I use this scirpt it just instantiates sparks in center of hit object, some of them are big and sparks appear too far from collision point. Is there any way to spawn them in the exact contact point?
Yes, but note that Location will need to be a Vector3 rather than a Transform. You would declare it:
private Vector3 Location;
You would assign it by:
Location = col.contacts[0].point;
Then you would use it:
Effects = Instantiate(Sparks, Location, Location.rotation);
Note that by convention, variables start with lower case letters. All of your variables start with upper case letters. The compiler does not have problems, but people reading your code might have issues.
can you provide the final working script please
Your answer
