- Home /
iTween Path swap
Hi guys, currently working on a runner game which uses lanes rather than movement. I've been working with the iTween scripts and have come up with a way to switch the lanes, however I can't get the player to stay on the new lane or move to it (not just appear). Any help is greatly appreciated.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
//private float smoothing = 1.0f;
private Vector3 targetPosition;
public float runSpeed = 0;
public Transform[] path;
public Transform[] path2;
public Transform[] path3;
float percentsPerSecond = 0.02f; // %2 of the path moved per second
float currentPathPercent = 0.0f; //min 0, max 1
// Use this for initialization
void Start ()
{
}
void Update ()
{
currentPathPercent += percentsPerSecond * Time.deltaTime;
iTween.PutOnPath(gameObject, path, currentPathPercent);
transform.LookAt(iTween.PointOnPath(path,currentPathPercent+.05f));
if (Input.GetKey ("a"))
{
iTween.PutOnPath(gameObject, path3, currentPathPercent);
transform.LookAt(iTween.PointOnPath(path3,currentPathPercent+.05f));
}
if (Input.GetKey ("d"))
{
iTween.PutOnPath(gameObject, path2, currentPathPercent);
transform.LookAt(iTween.PointOnPath(path2,currentPathPercent+.05f));
}
}
void OnDrawGizmos(){
iTween.DrawPath(path);
iTween.DrawPath(path2);
iTween.DrawPath(path3);
}
}
I'm not sure what you want to have happen here. Currently the character stays on 'path' unless you hold down the 'a' key or the 'd' key. What do you want to happen? Do you want it to switch from one to another?
sorry for the poor explanation- Yes, I would like the character to switch from one lane to another and stay there unless another key is pressed.
how do you make paths work as an endless runner? the paths have to be a finite length for iTween, correct?
@moconno - you should ask a new question rather than tagging a comment on an old question. As for iTween, he is using iTween.PutOnPath() and iTween.PointOnPath(). These are evaluated anew each time they are called, so the path can be changed from one frame to the next. Getting the fraction right for when the path was modified would take a bit of planning.
@moconno - The game concept was that of an endless runner however I has now been changed to a railshooter with multiple paths. Only issue is, is that I need the character to 'move' rather than 'appearing' on the next path and be able to jump and I'm all set.
Answer by robertbu · Mar 01, 2013 at 03:38 PM
Something like this (untested):
public class Movement : MonoBehaviour {
//private float smoothing = 1.0f;
private Vector3 targetPosition;
public float runSpeed = 0;
public Transform[] path1;
public Transform[] path2;
public Transform[] path3;
private Transform[] currPath;
float percentsPerSecond = 0.02f; // %2 of the path moved per second
float currentPathPercent = 0.0f; //min 0, max 1
void Start () {
currPath = path1;
}
void Update () {
if (Input.GetKeyDown("a")) {
currPath = path1;
}
else if (Input.GetKeyDown("s")) {
currPath = path2;
}
else if (Input.GetKeyDown("d")) {
currPath = path3;
}
currentPathPercent += percentsPerSecond * Time.deltaTime;
iTween.PutOnPath(gameObject, currPath, currentPathPercent);
transform.LookAt(iTween.PointOnPath(currPath,currentPathPercent+.05f));
}
void OnDrawGizmos(){
iTween.DrawPath(path1);
iTween.DrawPath(path2);
iTween.DrawPath(path3);
}
}
Cheers! Works Perfectly! Although after playing around with it the game controls would work better if the player movement was restricted by tapping only 'a' and 'd' to move around.
For example if the player is on path1 the have to tap 'd' twice to reach path3.
I've tried doing what I've got below but L'm not having any luck so far. if (currPath = path1) { (Input.Get$$anonymous$$eyDown("d")) { currPath = path2; } }
else {
if (currPath = path2)
{
(Input.Get$$anonymous$$eyDown("d"))
{
currPath = path3;
}
}
and same going back to path1 from path3 by tapping 'a' twice.
Any help you could offer would be greatly appreciated and once again thank you.
Untested:
public class $$anonymous$$ovement2 : $$anonymous$$onoBehaviour {
//private float smoothing = 1.0f;
private Vector3 targetPosition;
public float runSpeed = 0;
public Transform[] path0;
public Transform[] path1;
public Transform[] path2;
private Transform[][] paths = new Transform[3][];
private int iPath = 0;
float percentsPerSecond = 0.02f; // %2 of the path moved per second
float currentPathPercent = 0.0f; //$$anonymous$$ 0, max 1
void Start () {
paths[0] = path0;
paths[1] = path1;
paths[2] = path2;
}
void Update () {
if (Input.Get$$anonymous$$eyDown("a")) {
if (iPath == 0) iPath = 2; else iPath--;
}
else if (Input.Get$$anonymous$$eyDown("d")) {
iPath = (iPath + 1) % 3;
}
currentPathPercent += percentsPerSecond * Time.deltaTime;
iTween.PutOnPath(gameObject, paths[iPath], currentPathPercent);
transform.LookAt(iTween.PointOnPath(paths[iPath],currentPathPercent+.05f));
}
void OnDrawGizmos(){
iTween.DrawPath(path0);
iTween.DrawPath(path1);
iTween.DrawPath(path2);
}
}
Cheers for helping out again Robertbu, pressing 'd' makes the player cycle through the paths 0 to 1 to 2 and then back to 0. As for 'a' after it goes to path 0 and I press 'a' again it drops off the path.
EDIT: Just change the script around a bit and found the solution. Rather than;
void Update () {
if (Input.Get$$anonymous$$eyDown("a")) {
if (iPath == 0) iPath = 2; else iPath++;
}
else if (Input.Get$$anonymous$$eyDown("d")) {
iPath = (iPath + 1) % 3;
}
I changed it to;
void Update () {
if (Input.Get$$anonymous$$eyDown("a")) {
if (iPath == 2) iPath = 1; else iPath = 0;
}
else if (Input.Get$$anonymous$$eyDown("d")) {
if (iPath == 0) iPath = 1; else iPath = 2;
}
Now if I just get the character to 'move' rather than 'appearing' on the next path I'm all set.
Thank you again, you have helped me so much. :)
Fixed the code above. I changed iPath++ to iPath-- in the 'a' case.
Answer by Nethekurse · Dec 31, 2013 at 11:22 PM
Thanks i will try that, another thing is how can i make a closed path(loop)? It works with iTween.PutOnPath or i need use iTween.MoveTo ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613093553im_/https://answers.unity.com/themes/thub/images/avi.jpg)