I want my character move with a path after he collide with a collider, can this achieve by iTweenpath and ontriggerenter? Can anyone show me some codes with javascript for this case? thanks a lot~ happy coding
I am succeed to solve my case:
function OnTriggerEnter(coll:Collider){
canAnimate = true;
if(canAnimate) {
iTween.MoveTo(gameObject,iTween.Hash("path",iTweenPath.GetPath("path1"),"time",5));
}
}
but when seperate in function and update code, my character will be stuck in the start point of path and will not be moved with the path, any idea?
function OnTriggerEnter(coll:Collider){
canAnimate = true;
}
function Update(){
if(canAnimate) {
iTween.MoveTo(gameObject,iTween.Hash("path",iTweenPath.GetPath("path1"),"time",5));
}
}
thanks in advance for any advice~
Iām pretty sure the reason the character gets stuck at the start of the path when you try to start the animation from the Update()
function is because it is constantly restarting the animation on each frame, causing the character to be placed back at the start of the path.
Instead of putting the animation inside Update()
, you should put it in a different function which can be called from OnTriggerEnter()
. I would do it something like this:
function OnTriggerEnter(coll:Collider) {
if (canAnimate) {
// canAnimate has already been set to true, so we need
// to make sure the animation only gets started once
return;
}
canAnimate = true;
StartAnimation();
}
function StartAnimation() {
if (canAnimate) {
iTween.MoveTo(gameObject, iTween.Hash("path", iTweenPath.GetPath("path1"), "time", 5));
}
}