- Home /
How to get OnCollisionEnter working with Terrain. JavaScript
I am currently scripting a boat. I am using a box collider on the water, and a rigidbody on the boat so it looks like it's floating. There's also a Terrain collider on the terrain. I want my boat to stop when it hits the terrain, but right now I can't even get Unity to correctly detect that the boat has hit the terrain. Here's my code:
var speed : float = 5.0;
var rotateSpeed : float = 1.0;
function Update ()
{
//Move the boat forward
if(Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3(1 * speed *Time.deltaTime, 0, 0));
}
// Rotate the boat
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
//Plays Animation on the rod
if(Input.GetKeyDown('space'))
{
var rod = transform.Find("Pole (4)");
rod.animation.Play('Take 001');
}
}
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.tag == "Terrain")
{
Debug.Log("Hit");
}
}
Answer by Muuskii · Jul 20, 2012 at 07:08 PM
From your description: "I am using a box collider on the water so it looks like it's floating."
Are you saying that the water is a box collider or the boat is? Do you have a collider attached to the boat? If not then you won't receive collision events for it.
I have a box collider on the water and a rigidbody on the boat.
So you probably want to add a box collider over the parts of the boat that don't go in the water. Something can only collide with the terrain if it has a collider.
Thanks, you know how to make an accurate system so the boat stops moving when it hits the land?