- Home /
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;
}
Answer by zer0c00l42066 · Nov 29, 2013 at 08:49 PM
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.
Answer by tanoshimi · Nov 29, 2013 at 08:37 PM
Does your terrain have a collider on it too?
On line 8, try changing
isCollided === false
toisCollided == 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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123056im_/https://answers.unity.com/themes/thub/images/avi.jpg)