- Home /
put Camera on path with touch &/or arrow keys
Hello people Super grateful Robertbu helped me get my iTween Path working with Touch on my iOs devices, but now what if it I am on running on desktop...?
I want to use my arrow keys (either Up & Down or Left & Right) since it is on an iTween path and is simply bi - directional...
Here is the code I put on the Camera:
using UnityEngine;
using System.Collections;
public class FlythroughCameraController1 : MonoBehaviour {
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.Moved) {
percentage += touch.deltaPosition.x * speed / Screen.height;
percentage = Mathf.Clamp01(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;
}
}
I'm a but lost with how the touch phase is using percentage & how I might create an "else if" Left or Right Arrow keys...?
if(Input.GetKey(KeyCode.RightArrow))
{
something something
}
if(Input.GetKey(KeyCode.LeftArrow))
{
something something
}
Thanks for taking time to look,
~ be
Hello Bentoon, im looking for some way to make camera movement follow a path and seem your idea very interesting. Could you show all item to do camera follow a path in iOS device?
Thanks
Answer by robertbu · Aug 24, 2014 at 10:27 PM
if(Input.GetKey(KeyCode.RightArrow))
{
percentage += speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
percentage -= speed * Time.deltaTime;
}
percentage = Mathf.Clamp01(percentage);
'speed' is a variable you define. Start by assigning it a value of 1.0f.
but It's in the Update function on Camera ?
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);
iTween.PutOnPath(gameObject,movePath,percentage);
iTween.PutOnPath(lookTarget,lookPath,percentage);
transform.LookAt(iTween.PointOnPath(lookPath,percentage));
}
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
percentage += speed * Time.deltaTime;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
{
percentage -= speed * Time.deltaTime;
}
percentage = $$anonymous$$athf.Clamp01(percentage);
}
Thanks Rb,
Yes, in Update(). Note you have what are really two different speeds here. One used for touch and one used for arrow keys. Rename and reassign one of them. The need to be separate values. For example in the new code fragment, rename 'speed' to 'arrowSpeed'.
Got it ! Thanks Rb, again, again
Here is the working code if others come apron this thread.
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));
}
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
percentage += speed * Time.deltaTime;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
{
percentage -= speed * Time.deltaTime;
}
percentage = $$anonymous$$athf.Clamp01(percentage);
iTween.PutOnPath(gameObject,movePath,percentage);
iTween.PutOnPath(lookTarget,lookPath,percentage);
transform.LookAt(iTween.PointOnPath(lookPath,percentage));
}
Your answer
Follow this Question
Related Questions
Lerping between Camera Paths 1 Answer
[SCREENSHOTS] itween Path, orientToPath and gameObject rotation, 2D game 3 Answers
itween MoveTo IndexOutOfRangeException 0 Answers
How to create homing missile that follows path using Unity and iTween 1 Answer
How do Move a gameobject along a path WITHOUT A SPLINE 1 Answer