• 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
Question by patrick13 · May 17, 2015 at 08:28 AM · itweenpath

ITWEEN PointOnPath js converted C#

Hello I have this script on JS for ITWEEN PointonPath. He works.

     #pragma strict 
     public var target_Path1 : GameObject; 
     public var time = 33.0;         
     public var fraction = 0.0; 
     
     
     
     
     private var tabt : Transform[];
      private var running = false; 
      private var paused = false;
      
      
      function SetPath (pathObject : GameObject){
          var id : int = 0;
          tabt =null;
          tabt = new Transform[pathObject.transform.childCount];
          for(var child: Transform in pathObject.transform)     {
                 tabt[id++] = child.transform;
                      }
       WalkThePath();
                                                }
       
      
      
      function WalkThePath() {    
      paused = false;
      running = true;
      fraction = fraction;  
       while (running) {
              transform.position = iTween.PointOnPath(tabt, fraction);
              
      if (!paused)
               fraction += Time.deltaTime / time;
      if (fraction > 1.0) fraction = 0.0; 
     
             var futurePos = iTween.PointOnPath(tabt, fraction + 0.0000000001); 
             transform.LookAt(futurePos); 
                    yield;
                        } 
                           }
                  
     
     
     
     function Start()
         {
                SetPath(target_Path1);    
         } 
        
 

I want converted on C# but C# don't does not know "Yield"

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class TrajetVl : MonoBehaviour {
 
 public GameObject target_Path;
  public float time = 29.8f;
  public float fraction;
  
 
  public Transform[] tabt;
  private bool running = false;
  private bool paused = false;
 
  void Start () {
   
   SetPath(target_Path);
  }
 
  void SetPath (GameObject pathObject) {
   int id = 0;
   tabt = new Transform[pathObject.transform.childCount];
   foreach (Transform child in pathObject.transform) {
    tabt[id++] = child.transform;
   }
 
   WalkThePath ();
  }
 
  void WalkThePath () {
   bool paused = false;
   bool running = true;
 
   while (running) {
 
             
    transform.position = iTween.PointOnPath(tabt, fraction);
    if (!paused){ 
     fraction += Time.deltaTime / time;
    if (fraction > 1.0f){
     fraction = 0.0f;
     running = false;    
                 }
    transform.LookAt(iTween.PointOnPath(tabt, fraction + 0.0000000001f));
    transform.LookAt(futurePos);
    yield;
    }
   }
  }
 }


Can you help me for this problem ?

Thank you

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image

Answer by smallbit · May 17, 2015 at 12:38 PM

In c# you use yield in method of return type IEnumerator, it is called coroutine. more info here http://docs.unity3d.com/Manual/Coroutines.html

in your case:

 IEnumerator WalkThePath () {
 
 //code 
 
 yield return null; //stop execution till next frame
 }

and you start it

 StartCoroutine(WalkThePath());
 
 or
 
 StartCoroutine("WalkThePath"); 



the difference is in second case you can stop it using StopCoroutine.

Comment
Bunny83
patrick13

People who like this

2 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 Bunny83 · May 17, 2015 at 12:59 PM 0
Share

You can stop the coroutine even in the first case (since Unity 4.5 i think) as long as you store the returned Coroutine object. The first way is the preferred way since it avoids runtime errors (due to mistyped method names) and is in general a bit faster and more flexible.

avatar image

Answer by patrick13 · May 26, 2015 at 05:43 PM

Hi I try this but it's no work I have Unity 4.1.5f1 using UnityEngine; using System.Collections; using System;

 //se met automatiquement sur les vl
 public class TrajetVl : MonoBehaviour {
 
     public GameObject target_Path;
     public float time = 29.8f;
     public float fraction;
     
     public Transform[] tabt;
     private bool running = false;
     private bool paused = false;
     
  void Start () {
   
   SetPath(target_Path);
  }
 
  void SetPath (GameObject pathObject) {
   int id = 0;
   tabt = new Transform[pathObject.transform.childCount];
   foreach (Transform child in pathObject.transform) {
    tabt[id++] = child.transform;
   }
 
   StartCoroutine("WalkThePath");
  }
 
     IEnumerator WalkThePath () {
         bool paused = false;
          bool running = true;
     
          while (running) {            
                transform.position = iTween.PointOnPath(tabt, fraction);
                if (!paused)
                 fraction += Time.deltaTime / time;
                if (fraction > 1.0f)
                 fraction = 0.0f;                
         }
            transform.LookAt(iTween.PointOnPath(tabt, fraction + 0.0000000001f));
         yield return null;
     }
 }
 
 /*
  * public var target_Path1 : GameObject; // donner le path de MAYA
 public var time = 29.8;         // donner le temps de parcours, variable modifiable par autre script avec ''static '' public staticvar time = 10;
 //private var InitTime = time;  ??       // variable qui permet de revenir à la vitesse initiale EGALE à ''time''
 
 public var fraction = 0.0; //  variable de WalkOnPath qui donne la position sur le path en %
 
 
 
 
 public var VentreegiratoireSeuil = 70.0;    // vitesse fixe pour toutes les entrées
 public var VsortiegiratoireSeuil = 30.0;    // vitesse fixe pour toutes le sorties, mettre un cube vert un peu après dans le maquette
 
 
 
 
 private var tabt : Transform[];
  private var running = false; 
  private var paused = false;
  
  
  function SetPath (pathObject : GameObject){
      var id : int = 0;
      tabt =null;
      tabt = new Transform[pathObject.transform.childCount];
      for(var child: Transform in pathObject.transform)     {
             tabt[id++] = child.transform;
                  }
   WalkThePath();
                                            }
   
  
  
  function WalkThePath() {    
  paused = false;
  running = true;
  fraction = 0.1;  //  1er départ de la vidéo debut du parcours si 0.8 alors à 80%  (fraction = Pourcentage; ??) 
 
   while (running) {
          transform.position = iTween.PointOnPath(tabt, fraction);
          
  if (!paused)
           fraction += Time.deltaTime / time;
  if (fraction > 1.0) fraction = 0.0; // si 0.0 alors aller simple, le trajet reprend au 1° point
                                                    // si 1 trajet repart dans l'uatre sens
 
 
         var futurePos = iTween.PointOnPath(tabt, fraction + 0.0000000001); 
         transform.LookAt(futurePos); 
                yield;
                    } 
                       }
              
 
 
 
 function Start()
     {
            
            SetPath(target_Path1); 
           
     } 
    
    function Awake ()
    {
    target_Path1 = GameObject.Find("Repere_Trajet_E");
    }*/
Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How do you get the current frame of an iTweenPath in motion? 1 Answer

iTween multiple path? 1 Answer

Need to change iTween Path End Node position to Right Edge of the Screen 0 Answers

Does iTweenpath support OnTriggerEnter? 2 Answers

How can I make my iTweenpaths always visible in the editor ? 1 Answer


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