- Home /
iTweenPath auto keep on terrain
Hello everyone.
I'm using iTweenPath a lot, but I'm having a bit of trouble.
Right now, when I set a path, the gameobject is moving in the air.
I know I can make it move correctly along the terrain by placing the path nodes precisely on the terrain, but that will take a long time sense I have a long path and a really uneven terrain.
I was wondering if there might be a way to auto adjust the postion of the gameobject while it's moving along the path.
Thanks.
Answer by robertbu · Feb 24, 2013 at 12:47 AM
You could place and empty game object on the path and make your visible object a child of the empty game object. Each frame, you could raycast down to determine the elevation of the terrain and adjust the 'y' value in the visible objects in the transform.localPosition.
That sounds like it might work.
You $$anonymous$$d giving me an example code?
Here is the raycast bit (untested):
Ray ray = new Ray(trandform.position, Vector3.down);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)){
v3Pos = transform.position;
v3Pos.y = hit.point.y + myHalfHeight;
transform.position = v3Pos;
}
Depending on what other object you have in the scene and how you want your character to behave, you may want to use layers and a mask parameter to the Raycast.
Ok, so just to make sure I got it.
I make an empty gameobject which is moved by the iTweenpath.
Inside the empty gameobject, I place my actual gameobject with the with raycast code?
For everyone that is looking for something similar.
I came up with this:
v3Pos = transform.position;
v3Pos.y = (Terrain.activeTerrain.SampleHeight (v3Pos)) + myHalfHeight;
transform.position = v3Pos;
This one dosn't use raycast.