- Home /
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
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?
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
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?
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
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.
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
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