- Home /
Camera animation using EasyTouch's swipe and iTween
Hi all,
I am using EasyTouch for swiping and I wanted to have a swipe animation for my camera using iTween.
In simple words, I wanted is a perfect swipe for my game, in which my camera will move a little close to target transform when swipe is small or move farther when swipe is big. The target here will be the fixed transform position, from where the camera transform could not go beyond.
Currently I am doing this :
public Transform targetRightCam;
public Transform targetLeftCam;
public float transSpeed;
private void On_Swipe(Gesture gesture)
{
if(gesture.touchCount == 1)
{
if (gesture.swipe == EasyTouch.SwipeType.Right)
{
target = targetLeftCam;
iTween.RotateTo(gameObject, iTween.Hash("rotation", target.eulerAngles, "time", transSpeed));
iTween.MoveTo(gameObject, iTween.Hash("position", target.position, "time", transSpeed));
}
else if(gesture.swipe == EasyTouch.SwipeType.Left)
{
target = targetRightCam;
iTween.RotateTo(gameObject, iTween.Hash("rotation", target.eulerAngles, "time", transSpeed));
iTween.MoveTo(gameObject, iTween.Hash("position", target.position, "time", transSpeed));
}
}
}
It works, but not the way I wanted as it is not based on swipe distance, it just moves the camera to the target position in a one single swipe event(whether small or big). So please if anyone has some suggestions or answers, please help. Also I am newbie to iTween library.
Answer by DannyB · Jul 18, 2013 at 03:26 PM
A few suggestions:
As for the touch library:
I am not familiar with EasyTouch, but I could not find anything in its sparse documentation that tells me if its swipe detection provides the caller with information about the swipe (e.g. distance). If it does, you need to use it. If it does not, you need to look for another method in its documentation that provides that, or switch to another gesture detection library (Input.Touches is one that I can vouch for).
In some libraries, a Drag gesture may be closer to what you need, as it both provides continuous events as well as start point and end point of the gesture.
As for the tweening
Although I am a big fan of iTween I found out that it is better to avoid it in some situations. Some points for consideration:
If you may want to interrupt the tween (e.g. user swipes left then immediately right) - avoid iTween.
If the tweening is simple enough (and therefore can be replaced with SmoothDamp or something similar) - avoid iTween.
If it is mission critical (e.g. happens a lot, or its timing is critical) - avoid iTween.
Some pseudo logic
In your case, I feel like you simply need to find the right tools for the job, but the logic should be the same:
OnSwipe( SwipeInfo swipe ) {
if( swipe.direction == Right ) {
if( swipe.distance < myThreshold ) {
...
}
else {
...
}
}
if( swipe.direction == Left ) {
if( swipe.distance < myThreshold ) {
...
}
else {
...
}
}
}
I know this is not a concrete answer, but I hope it helps in some way to put you on teh right track.
To answer your additional question, this is how an iTween call may look like if you want to use the target's position and speed:
GameObject target;
iTween.MoveTo( gameObject, iTween.Hash(
"position", target.transform.position,
"speed" , target.speed
));
This of course assumes that target
has a publicly accessible speed
variable.
thanks for the cool suggestions, ok so could you help me with iTween more. Is there any function in iTween which could move my gameObject to target( by passing target's position and the moving speed of target, as a parameter of HashTable)..?
It looks like you are already doing it in the code in your question. You are only using "time" ins$$anonymous$$d of "speed" (which is fine if that is what you want).
In any case, added some more stuff to the answer.
ok yeah, sorry for that, I started using iTween from yesterday only, I've not seen that :p
Hi @DannyB, I m here again..:( and basically here is the thing : I have a room, and I want the user to move inside the room by swiping on his phone screen. There will be some fixed target positions(lets say 3 now) in which the user can move. So on first swipe, the user moves to its fixed target position, and on swiping again, he moves to the next fixed target position, and so on.. so could you provide me some sort of script, which does somewhat similar animation to what I want to achieve, with my camera animations.
Well, this discussion is quickly moving away from the original question and is more suitable for the forums than the Answers. If you have a particular coding problem, Answers is suitable, otherwise, the forums.
But as a quick tip, I can tell you that two approaches come to $$anonymous$$d:
Have a
GameObject nextTarget
in your script, and change it to point to the next target. This variable can either change by an inside trigger, or any outside source.Have a generic list or queue or array (as suitable) of game objects, which will hold all the targets, and something (again internal or external) will change its index.
In both cases, the iTween simply takes this current target as its destination.