OnCollisionEnter help!

Hi, I’m new to U3d and JavaScript and trying to make a project here.

What I want to do is when a rock falls down onto the terrain, a crater generates on that spot of the terrain.

My question is when touch = true, how can I send the position of the collision to the function DeformTerrain? I know my coding has many problems, please give some suggestions. Thanks.

function OnCollisionEnter (collision : Collision)
{
    var contact; 
    if (collision.gameObject.tag == "rock")
   {
       touch = true;
	   contact = collision.contacts[0];
	   thisPositon = contact.transform;
	}
}

function Update () 
{
  
}

function LateUpdate ()
{
    if (touch)
   {
	   DeformTerrain (thisPositon);
	   touch = false;
    }
}

function DeformTerrain (pos : Transform)
{
  // generate a crater here

}

http://en.wiktionary.org/wiki/carter

Which one is better ?

*crater

Thanks.

That would be collision.contacts[0].point. See Unity - Scripting API: Collision.contacts

One thing is that Update is for things that happen every frame, and you would only want that to happen on collision, so remove the Update functions and just put the crater code in OnCollisionEnter, or call it once from there. Also don’t do things like “var contact;”, since that uses dynamic typing (slow) and there’s no reason for it here.

–Eric

Thanks Eric!

I modified the code. Is this what you suggested?

function OnCollisionEnter (collision : Collision) 
{ 
    if (collision.gameObject.tag == "rock") 
   { 
      //touch = true; 
      contact = collision.contacts[0].point; 
      DeformTerrain (contact); 
   } 
} 

function DeformTerrain (pos : Transform) 
{ 
  // generate a crater here 

}