• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Bentoon · Aug 14, 2015 at 09:52 PM · animationcamera-movementitweentouchscreenitweenpath

tween Path animation & touch control

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

Comment

People who like this

0 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmgek · Aug 14, 2015 at 11:29 PM 0
Share

We don't know what you are asking and there is too much code to look through, upload the parts you need people to look at and that may help.

avatar image Bentoon · Aug 15, 2015 at 01:57 AM 0
Share

Thanks jmgek I wasn't able to comment before so I had to post it as an answer, See below

1 Reply

  • Sort: 
avatar image

Answer by Bentoon · Aug 15, 2015 at 01:06 AM

I hear that!

Let me try again: I have a Camera script that allows me to use touch to control its movement along an iTween path [movPath] I am also controlling where it looks on another iTween path [lookPath]

My Touch on the screen controls the movement of the camera along the path Now I want the camera motion along the path to be animated as well with my touch just accelerating or decelerating it

Does this make more sense?

Thanks!

         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(){
                  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 () 
              {
     
          
                  if (Input.touchCount > 0&&canControl) {
          
                      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;
                          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));
          
                      }
          
                  }
          
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does iTweenpath support OnTriggerEnter? 2 Answers

Itween 180º curve position and rotation problem 0 Answers

Update iTween Path at Runtime 1 Answer

How can I save an iTween animation as a .anim file? 1 Answer

iTween Get the path from object nodes and move the object at same speed by loop 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges