Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Bentoon · Aug 19, 2014 at 03:23 AM · touchitweenpathfollowingarrow-keys

iTween, Put on path & arrow keys

Hello people

I am using iTween to put the camera on a path

but I want to control it with touch instead of a "Slider"...

Here is the what we have:

 using UnityEngine;
 using System.Collections;
 
 public class PutOnPathExample : MonoBehaviour{
     public Transform[] path;
     public float percentage;
     
     void OnGUI () {
         percentage=GUI.HorizontalSlider(new Rect(23,194,204,40),percentage,0,1);
         iTween.PutOnPath(gameObject,path,percentage);
         
         //You can cause the object to orient to its path by calculating a spot slightly ahead on the path for a look at target:
         transform.LookAt(iTween.PointOnPath(path,percentage+.05f));
     }
     
     void OnDrawGizmos(){
         iTween.DrawPath(path);
     }
 }
 



I have Also had luck mapping to my arrow keys for touch... So here is the script I am using for that:

 #pragma strict
 
 // Moves object according to finger movement on the screen
 
     var speed : float = 0.05;
     var nonSpeed : float = 0;
     
     function Update () {
         if (Input.touchCount > 0 && 
           Input.GetTouch(0).phase == TouchPhase.Moved) {
         
             // Get movement of the finger since last frame
             var touchDeltaPosition:Vector3 = Input.GetTouch(0).deltaPosition;
             
             // Move object across X plane
             transform.Translate (-touchDeltaPosition.x * speed,
             0,0);
             
             transform.Translate (-touchDeltaPosition.z * speed,
             0,0);
 
         
         }
     }
 


I know the scripts are C# & .js respectively ... but Any advice is helpful

Thanks for your time

~be

Comment
Add comment · Show 4
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 robertbu · Aug 19, 2014 at 04:23 AM 0
Share

want to control it with touch ins$$anonymous$$d of a "Slider"...

What does this mean? Do you want the user to pull it along the path? What if the path diverges from the finger? Is the path primary one direction?

avatar image Bentoon · Aug 19, 2014 at 10:54 AM 0
Share

Hello RobertBu

Good question...

I want to control the back and forth along the path with both a swiping touch on a phone and on a computer with the arrow keys ins$$anonymous$$d of the slider.

the touch code I included in the second example works on a computer with both a swiping gesture and the Left & Right arrow keys....(although I plan on using the z axis and up & down arrow keys in this new test)

Does that make sense?

Thanks for checking

~be

avatar image robertbu · Aug 19, 2014 at 03:56 PM 0
Share

You want a horizontal swipe correct? And is the path horizontal with respect to the view, or arbitrary? How far should a swipe take the user along the path?

avatar image Bentoon · Aug 20, 2014 at 02:35 PM 0
Share

sorry for the delay RobertBu,

I have been working long hours. Sorry for the confusion .

Basically I have the slider script controlling the Camera on a path, but I want to remove the slider and have it controlled with gesture (almost as if the slider was bigger & invisible)

If the user is at a computer I want you to be able to use the arrow keys to go forwards & back along the path...


So I included the Touch script I use, because it lows me to use arrow keys on a computer or a horizontal / vertical touch gesture ...

Thanks as always, for any insight

~be

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Aug 20, 2014 at 05:57 PM

I'm away from my desktop, so I cannot test it, but here is some sample code in the right direction:

 public Transform[] path;
 public float percentage;
 public float speed = 0.1f;
 
 void Update () {
     if (Input.touchCount > 0) {
     
         Touch touch = Input.GetTouch(0);
         
         if (touch.phase == TouchPhase.Moved) {
             percentage += touch.deltaPosition.x * speed / Screen.height;
             percentage = Mathf.Clamp01(percentage);
             iTween.PutOnPath(gameObject,path,percentage);
             transform.LookAt(iTween.PointOnPath(path,percentage+.05f));
         }
     }
 }

  • I have no idea what the value of 'speed' should be. You'll have to experiment.

  • Note the division by Screen.height. This is a quick and dirty of normalizing the movement for different resolutions. You can use Screen.dpi as another way to handle the resolutions.

  • This is looking at the horizontal movement. It would take some changes for an arbitrary swipe.

Comment
Add comment · Show 2 · 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
avatar image Bentoon · Aug 21, 2014 at 03:51 AM 0
Share

Thanks So much RobertBu for your fast attention. it means a lot! I will have to check this tomorrow when I am off shift...

Looking forward

~be

avatar image Bentoon · Aug 23, 2014 at 04:39 AM 0
Share

RobertBu, Once again you come to the rescue! I modded the script and it works I'm still not altogether sure how ; ) there are other elements. But NOW I can retrace and begin to really understand here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class FlythroughCameraController1 : $$anonymous$$onoBehaviour {
     public Transform[] movePath;
     public Transform[] lookPath;
     public Transform lookTarget;
     public float percentage;
     //public Transform[] path;
     public float speed = 0.1f;
 
 
     private float redPosition = .16f;
     private float bluePosition = .53f;
     private float greenPosition = 1;
 //    private float purplePosition = 1.2;
     
     //gui styling
     public Font font;
     private GUIStyle style = new GUIStyle();
     
     void Start(){
         style.font=font;
     }
 
     void Update () {
         if (Input.touchCount > 0) {
             
             Touch touch = Input.GetTouch(0);
             
             if (touch.phase == TouchPhase.$$anonymous$$oved) {
                 percentage += touch.deltaPosition.x * speed / Screen.height;
                 percentage = $$anonymous$$athf.Clamp01(percentage);
                 //Robertbu    iTween.PutOnPath(gameObject,path,percentage);
                 iTween.PutOnPath(gameObject,movePath,percentage);
                 iTween.PutOnPath(lookTarget,lookPath,percentage);
                 transform.LookAt(iTween.PointOnPath(lookPath,percentage));
                 //transform.LookAt(iTween.PointOnPath(path,percentage+.05f));
             }
         }
     }
 
 
     void OnDrawGizmos(){
         iTween.DrawPath(movePath,Color.magenta);
         iTween.DrawPath(lookPath,Color.cyan);
         Gizmos.color=Color.black;
         Gizmos.DrawLine(transform.position,lookTarget.position);
     }
     
     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;
     }
 }
 



$$anonymous$$y only other question would be how - in the Update method, would I ilcude an "else if Left or Right Arrow key

     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
     {
         something something
     }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
     {
         something something
     }
 

Thank you again! ~be

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Raycast Hit not working 0 Answers

Touch and drag object on path with iTween 0 Answers

iTween Path-Constrained character loop trouble 0 Answers

How to control an animation by touch position along a path? 1 Answer

iTween path constrained character: force original orientation? 0 Answers


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