Hello All
I have a script on my Camera that allows me to use touch to control its movement [movPath] and where it is looking [lookPath]
Now I want a slight animation on the camera as well
So that it moves along the movePath but I can still use touch to faster or slower or even reverse
Any Ideas?
using UnityEngine;
using System.Collections;
//collection of a moving path and a looking path
//holds the information for a pair of look/move paths.
[System.Serializable]
public class moveAndLookUnits_1
{
public Transform[] movePath;
public Transform[] lookPath;
}
public class FlythroughCam_Animate : MonoBehaviour {
//creates a set of movepaths
public moveAndLookUnits_1[] moveAndLookPaths;
public Transform lookTarget;
//saves the position along the path.
public float percentage = .1f;
float storedpercentage=0f;
//public Transform[] path;
public float speed = 0.1f;
public float shiftspeed=2;
public int pathLength=3;
//variables not in interface.
// do not change in editor
public int whichPath = 0;
bool canControl = true;
float toggle = 0f;
Vector3 movePointA= new Vector3();
Vector3 movePointB=new Vector3();
Vector3 lookPointA=new Vector3();
Vector3 lookPointB=new Vector3();
float toggleMoveDistance = 0f;
float toggleLookDistance = 0f;
float distanceToggled= 0f;
float alongMoveDistance=0f;
float alongLookDistance=0f;
float distanceMovedalong=0f;
int nextPath=0;
void Start(){
// OnDrawGizmos();
tween();
}
// BELOW IS A FAST ATTEMPT TO ANIMATE the Cam while still having touch control:
void tween(){
// iTween.MoveTo(gameObject,iTween.Hash("path",moveAndLookPaths[whichPath].movePath,"time",7,"orienttopath",true,"looktime",.6,"easetype","easeInOutSine","oncomplete","complete"));
}
void Update ()
{
//Debug.Log ("update" + Input.touchCount+ " touchtype "+ " path "+ whichPath);
// canContrl is a Bool
// whichPath holds the progressive # of the path on the camera
if (Input.touchCount > 0&&canControl) {
// if (Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved) {
alongMoveDistance=iTween.PathLength(moveAndLookPaths[whichPath].movePath);
alongLookDistance=iTween.PathLength(moveAndLookPaths[whichPath].lookPath);
distanceMovedalong=touch.deltaPosition.x*speed;
//Debug.Log (distanceMovedalong);
percentage -=distanceMovedalong/alongMoveDistance;
percentage = Mathf.Clamp01(percentage);
iTween.PutOnPath(gameObject,moveAndLookPaths[whichPath].movePath,percentage);
iTween.PutOnPath(lookTarget,moveAndLookPaths[whichPath].lookPath,percentage);
transform.LookAt(iTween.PointOnPath(moveAndLookPaths[whichPath].lookPath,percentage));
//transform.LookAt(iTween.PointOnPath(path,percentage+.05f));
}
}
//the space bar will toggle between the paths. In the case of multiple paths, it will go one by one through
//the array, and then when it gets to the last path, it will move back to the first one.
//by toggle, it will lerp to the new point
if(Input.GetKey (KeyCode.Space)&&canControl)
{
nextPath=(whichPath+1)%moveAndLookPaths.Length;
movePointA= iTween.PointOnPath(moveAndLookPaths[whichPath].movePath, percentage);
movePointB= iTween.PointOnPath(moveAndLookPaths[nextPath].movePath, percentage);
lookPointA= iTween.PointOnPath(moveAndLookPaths[whichPath].lookPath, percentage);
lookPointB= iTween.PointOnPath(moveAndLookPaths[nextPath].lookPath, percentage);
canControl=false;
toggleMoveDistance=(movePointA-movePointB).magnitude;
toggleLookDistance =(lookPointA-lookPointB).magnitude;
}
if(Input.GetKey(KeyCode.UpArrow)&&canControl)
{
percentage -= speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.DownArrow)&&canControl)
{
percentage += speed * Time.deltaTime;
}
if(toggle>.999f&&toggle<1.001f)
{
whichPath=nextPath;
if(whichPath==0)
{
//percentage = storedpercentage;
}
canControl=true;
iTween.PutOnPath(gameObject,moveAndLookPaths[nextPath].movePath,percentage);
iTween.PutOnPath(lookTarget,moveAndLookPaths[nextPath].lookPath,percentage);
transform.LookAt(iTween.PointOnPath(moveAndLookPaths[nextPath].lookPath,percentage));
toggle= 0f;
distanceToggled=0f;
//Debug.Log ("Arrived");
}
if(canControl)
{
percentage = Mathf.Clamp01(percentage);
iTween.PutOnPath(gameObject,moveAndLookPaths[whichPath].movePath,percentage);
iTween.PutOnPath(lookTarget,moveAndLookPaths[whichPath].lookPath,percentage);
transform.LookAt(iTween.PointOnPath(moveAndLookPaths[whichPath].lookPath,percentage));
}
else
{
//for going between 2 paths:
//set as a fixed speed per second vs as a ratio of the path length;
distanceToggled+=shiftspeed*Time.deltaTime;
toggle=distanceToggled/toggleMoveDistance;
if(toggle>1) toggle=1;
iTween.PutOnPath(gameObject, new Vector3[]{movePointA, movePointB}, toggle);
iTween.PutOnPath(lookTarget,new Vector3[]{lookPointA, lookPointB},toggle);
transform.LookAt(iTween.PointOnPath(new Vector3[]{lookPointA, lookPointB},toggle));
// Debug.Log(toggle);
}
}
public void ToggleToPath(int pathNo, float newPercentage=0f)
{
nextPath=(pathNo);
storedpercentage=percentage;
Debug.Log (newPercentage);
movePointA= iTween.PointOnPath(moveAndLookPaths[whichPath].movePath, percentage);
movePointB= iTween.PointOnPath(moveAndLookPaths[nextPath].movePath, newPercentage);
lookPointA= iTween.PointOnPath(moveAndLookPaths[whichPath].lookPath, percentage);
lookPointB= iTween.PointOnPath(moveAndLookPaths[nextPath].lookPath, newPercentage);
Debug.Log (movePointB);
canControl=false;
percentage = newPercentage;
toggleMoveDistance=(movePointA-movePointB).magnitude;
toggleLookDistance =(lookPointA-lookPointB).magnitude;
}
public void ToggleToPathPreserve(int pathNo)
{
nextPath=(pathNo);
Debug.Log (storedpercentage);
//percentage = storedpercentage;
movePointA= iTween.PointOnPath(moveAndLookPaths[whichPath].movePath, percentage);
movePointB= iTween.PointOnPath(moveAndLookPaths[nextPath].movePath, storedpercentage);
lookPointA= iTween.PointOnPath(moveAndLookPaths[whichPath].lookPath, percentage);
lookPointB= iTween.PointOnPath(moveAndLookPaths[nextPath].lookPath, storedpercentage);
canControl=false;
toggleMoveDistance=(movePointA-movePointB).magnitude;
toggleLookDistance =(lookPointA-lookPointB).magnitude;
}
void SlideTo(float position){
iTween.Stop(gameObject);
iTween.ValueTo(gameObject,iTween.Hash("from",percentage,"to",position,"time",2,"easetype",iTween.EaseType.easeInOutCubic,"onupdate","SlidePercentage"));
}
void SlidePercentage(float p){
percentage=p;
}
void OnDrawGizmos(){
foreach(moveAndLookUnits_1 p in moveAndLookPaths)
{
iTween.DrawPath(p.movePath,Color.magenta);
iTween.DrawPath(p.lookPath,Color.cyan);
}
Gizmos.color=Color.black;
Gizmos.DrawLine(transform.position,lookTarget.position);
}
}
Thanks
~be