My boat goes through the terrain, how do I stop this?

I have programmed my boat to move, and it happily sails around, but it can go through the islands I have made out of the terrain. I have put a box collider around it, but that didn’t help. If my code needs to be changed, please tell me. This is my code:

var rock_isGoingToRight = true;
var rock_Amount = 0;
var isRockingBackwards = true;
var isCollided = false;

function Update() 
{
	if((Input.GetKey("up") || Input.GetKey("w")) && isCollided === false) {
		transform.Translate(Vector3.right * (Time.deltaTime*40)); 
	}
	
	if(Input.GetKey("right") || Input.GetKey("d")) {
		transform.Rotate(Vector3.forward * (Time.deltaTime*10)); 
	}
	
	if(Input.GetKey("left") || Input.GetKey("a")) {
		transform.Rotate(Vector3.back * (Time.deltaTime*10)); 
	}
	if(rock_isGoingToRight === true) {
		transform.Rotate(Vector3.right * Time.deltaTime);
		rock_Amount++;
		if(rock_Amount > 90) {
			rock_isGoingToRight = false;
		}
	} else {
		transform.Rotate(Vector3.left * Time.deltaTime);
		rock_Amount--;
		if(rock_Amount < -90) {
			rock_isGoingToRight = true;
		}
	}
}

function OnCollisionEnter(theCollision : Collision) {
	isCollided = true;
}

2 Answers

2

Try setting a Terrain collider to the terrain. then setting the terrain data to point to the terrain you are using…

if your boat is a mesh object use a mesh collider and attach the boat object to the mesh field.

I agree with the terrain and mesh collider solution

1.) Does your terrain have a collider on it too?

2.) On line 8, try changing isCollided === false to isCollided == false (or even just !isCollided)

I've tried what you said, but it still hasn't worked. I think its because the boat doesn't collide with anything as I put a cube in front of it, and my boat went straight through it. I've added colliders, but it still doesn't work.

In editor, move your boat little above terrain collider then hit play. See what happens.

And I assume your boat has a rigidbody component?