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 Alter · Feb 28, 2013 at 11:26 AM · c#itweenpath

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);
     }
 }
 
Comment
Add comment · Show 6
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 · Mar 01, 2013 at 05:52 AM 0
Share

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?

avatar image Alter · Mar 01, 2013 at 01:53 PM 0
Share

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.

avatar image moconno · Apr 17, 2013 at 10:40 PM 0
Share

how do you make paths work as an endless runner? the paths have to be a finite length for iTween, correct?

avatar image robertbu · Apr 18, 2013 at 02:39 AM 0
Share

@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.

avatar image Alter · Apr 22, 2013 at 05:35 AM 0
Share

@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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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);
     }
 }
Comment
Add comment · Show 9 · 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 Alter · Mar 02, 2013 at 10:45 AM 0
Share

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.

avatar image robertbu · Mar 02, 2013 at 04:36 PM 1
Share

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);
     }
 }
avatar image Alter · Mar 03, 2013 at 10:43 AM 0
Share

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.

avatar image Alter · Mar 03, 2013 at 10:47 AM 0
Share

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. :)

avatar image robertbu · Mar 03, 2013 at 10:49 AM 1
Share

Fixed the code above. I changed iPath++ to iPath-- in the 'a' case.

Show more comments
avatar image
1

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 ?

Comment
Add comment · Show 1 · 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 aceleeon5 · Jun 24, 2014 at 10:55 PM 0
Share

Any luck with this one?

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

13 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

Related Questions

Switching Between (many) different iTween Paths (C#) 0 Answers

How do I "instantiate" a path with iTween? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

With Unity 4, how do you get MonoDevelop to recognize a newly loaded plugin like iTween? 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