- Home /
Moving Over Terrain with iTween
Hi, I'm trying to move an animated creature over a terrain. I've been using iTween to do the waypoint patrolling, but I'm having trouble making the creature track the Y height of the terrain. Ideally I'd like it to follow the orientation of the terrain mesh, but at this point I'll settle for just tracking the Y.
What I've got is this:
function CheckGround(){
var hit : RaycastHit = new RaycastHit();
if (Physics.Raycast (transform.position,Vector3.down,hit, 2)) {
myRat.position.y=hit.point.y + 5;
}
}
which I'm calling from Update().
function tween(){
iTween.MoveTo(gameObject,{"path":path,"time":mySpeed,"orienttopath":true,"looktime":.6,"easetype":"easeInOutSine","oncomplete":"complete"});
}
which is called from Start();
path is an array of Transforms
So, my understanding of how this should work is that iTween fires up when the game starts and moves the creature through the waypoints in the path. Every frame of that, the creature raycasts downwards and, if it hits an object or the terrain, it will adjust its Y access to be the same as the point where the ray hit plus 5.
The creature is on the Ignore Raycast layer. The waypoint part is fine, but the Raycasting doesn't do anything, in fact the creature sinks slightly in the first few seconds. The size of the terrain is 500x500.
Ok, it occurs to me why this is going wrong (or rather not going right), but I have no idea how to fix it.
I'm now thinking that the iTween call, because it is moving the creature to a Vector3, is overriding the changes in transform.position.y that the raycasting function gives in order to keep the creature on track.
If that's the case, is there any way to tell iTween to ignore the Y component of the Vector3?
Answer by Thom Denick · Jul 20, 2011 at 04:38 PM
I know this is an old question, but Bob actually has a solution up for this exact problem on his site now. http://itween.pixelplacement.com/examples.php
$1 will get you the source.