- Home /
Object spawned during collision: Object's Y axis to be colliding object's polygon's direction
First off, sorry for the confusing title. Let me explain;
I'm spawning some particle effects and meshes during collision(Dust and rubble pieces used in shootouts). The particle effects have Y=30 as their local velocity. What I want to do is spawn the particle so that it's Y axis is facing the FACE/POLYGON/PLANE of the object it just collided with, so that the particle looks like it's coming off the surface of the polygon that made contact with the bullet. Here's a very simple piece of code that's assigned to my bullet prefab, so gameobject is the bullet itself:
if(hit.gameObject.tag == "environment"){
function OnTriggerEnter( hit : Collider ) { var bli = Instantiate(blistparticle, gameObject.transform.position, hit.gameObject.transform.rotation); var smk = Instantiate(dustparticle, gameObject.transform.position, hit.gameObject.transform.rotation);
Destroy(gameObject); } }
This is a fragment of the actual code as I have different particles spawned for different sort of surfaces that are in the game. I have tried a couple of ways to achieve my goal with no results...
Help!
Thanks in advance
Answer by PrimeDerektive · Feb 18, 2011 at 05:16 PM
You probably won't be able to accomplish it using OnTriggerEnter(), because it doesn't receive collision data.
You'll need to use OnCollisionEnter(collision : Collision), then align the the particle system prefab with the y axis of the normal of the point of contact.
Something like:
function OnCollisionEnter(collision : Collision) {
var contactPoint = collision.contacts[0];
var rot = Quaternion.FromToRotation(Vector3.up, contactPoint.normal);
var pos = contact.point;
Instantiate(particlePrefab, pos, rot);
}
It works well in low velocities but when I increase the speed it just goes through objects so the instantiating part always happens in the wrong place :(
I would check out DontGoThroughThings.js on the unify community wiki. http://www.unifycommunity.com/wiki/?title=DontGoThroughThings
I'd like you to know that experimenting with numerical values while using your suggestion worked. Thanks again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105041im_/https://answers.unity.com/themes/thub/images/avi.jpg)