Getting the right contact point.

So I have a ball object which gets thrown. When it touches something, it bounces off and on the place where the ball has collided with the object, occurs a particle system. To get the position and rotation of the particle system I do this:
var contact : ContactPoint = col.contacts[0];
Instantiate(hitEffect, contact.point, Quaternion.FromToRotation(Vector3.up, contact.normal));
The problem is that sometimes when I shoot the ball towards a wall, the particle system gets instantiated inside the wall.
I also tried:
var contact : ContactPoint = col.contacts[col.contacts.length-1];
…but without success. So how can I do this right ?

So using the example given in the documentation:

I came up with this code that I put on a box in my scene. (Mind you I added something to reset it and push it around because stopping the whole scene and restarting was not very fun)

var explosionPrefab : Transform;
var startPos : Vector3;

function Start(){
	startPos=transform.position;
}
function Update(){
	if(Input.GetKey("r")){
		transform.position=startPos;
		rigidbody.velocity=Random.insideUnitSphere.normalized * 50;
	}
}

function OnCollisionEnter(collision : Collision) {
	// Rotate the object so that the y-axis faces along the normal of the surface
	var contact : ContactPoint = collision.contacts[0];
	var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
	var pos : Vector3 = contact.point;
	Instantiate(explosionPrefab, pos, rot);
}

Now, I dont know if I answered your question, but it works and my prefab does not appear under my terrain. I would say your best bet is to build a small test scene and test just this part of it, if you have problems.

So you have basically done the same thing I did and you say there’s no problems? Well with my game mechanics something goes wrong. I have a FPS type player and this thing happens like 15% of the time. I’m assuming that it may be because it’s a rigidbody and I’m shooting it with fairly high speed so the ball gets inside the wall. However if this is the problem, how will I fix it without having an impact on the performance?

Does the bullet have to exist after the impact?

Hey guys, i’ve got the same problem with getting a contact point on the surface of a collider that we hit. We tried about everything. Simply put if you “shoot” GameObject with Rigidbody and a collider, into another collider, inside OnCollisionEnter (attached to this “bullet”) collision.contacts will return pretty random values if you hit the collider under angles other than perpendicular - contact point is far inside of it… Increasing size of the bullet (collider) from 1cm to higher values (at least 0.15m) seemed to reduce this “feature” a little bit, but not enough.

Any thoughts, hints or solutions please?

My suggestion is to drop rigidbodies for bullets altogether and use ray/linecasting instead. It’s much more reliable for really fast moving bullets and you can still pull it down with gravity and reflect it for ricochets. Another thing is it will give you the exact point on the collider so you can have your bullet holes and whatnot in the correct place.

We have some special effects on the bullet itself, not sure if we can make em happen without game object around, i’ll have to try it as backup plan… But the big WHY here still remains, why is contact point inside of the collider, form an angle?

What’s happening is your bullet is in front of the target object in one physics frame, then the next frame it’s all the way on the other side of the object because it was moving so fast. To fix this you can try increasing the frame rate of your physics simulation (Edit->Project Settings->Time), or you can try enabling continuous collision detection on for your fast moving rigidbodies. Both of these solutions can have pretty significant overhead.

The speed of the bullet is not that big and it still does not explain why would it hit the surface from some angles, and then from different angles it will appear far inside… while the distance from collider is the same. Speed is always the same (eg normalized direction * float, where float is 50) which still makes only one meter per frame (at 50fps) yet it can appear even several meters in - the most i’ve seen was way over 2…

Anyway I decided to just fire a ray instead, thanks for help though!

Ok cool - You don’t have to lose the game object to use it, see this trajectory example I posted a week ago:

You can still have trail effects or whatnot if you linecast with a vector for velocity and use that vector to move the gameobject