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 Halesey · Aug 26, 2014 at 08:51 PM · loopmovepath

How can i loop my path instead of it ending on the lasat point

I have two scripts in C#...

My "path definition"...

 using System;
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PathDefinition : MonoBehaviour
 {
     public Transform[] Points;
 
     public IEnumerator<Transform> GetPathEnumerator()
     {
         if (Points == null || Points.Length < 1)
             yield break;
 
         var direction = 1;
         var index = 0;
         while (true)
         {
             yield return Points[index];
 
             if(Points.Length == 1)
                 continue;
 
             if (index <= 0)
                 direction = 1;
 
         
 
             index = index + direction;
 
         }
     }
 
     public void OnDrawGizmos()
     {
         if (Points == null || Points.Length < 2)
             return;
 
         for (var i = 1; i < Points.Length; i++)
         {
             Gizmos.DrawLine(Points[i - 1].position, Points[i].position);
         }
     }
 
 
 
 and my "follow path"....
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class FollowPath : MonoBehaviour
 {
     public enum FollowType
     {
         MoveTowards,
         Lerp
     }
 
     public FollowType Type = FollowType.MoveTowards;
     public PathDefinition Path;
     public float Speed = 1;
     public float MaxDistanceToGoal = .1f;
 
     private IEnumerator<Transform> _currentPoint;
 
     public void Start()
     {
         if (Path == null)
         {
             Debug.LogError("Path cannot be null", gameObject);
             return;
         }
 
         _currentPoint = Path.GetPathEnumerator();
         _currentPoint.MoveNext();
 
 
         if (_currentPoint.Current == null)
             return;
 
         transform.position = _currentPoint.Current.position;
     }
 
     public void Update()
     {
         if (_currentPoint == null || _currentPoint.Current == null)
             return;
 
 
         if (Type == FollowType.MoveTowards)
             transform.position = Vector3.MoveTowards(transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);
         else if (Type == FollowType.Lerp)
             transform.position = Vector3.Lerp(transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);
 
         var distanceSquared = (transform.position - _currentPoint.Current.position).sqrMagnitude;
         if (distanceSquared < MaxDistanceToGoal * MaxDistanceToGoal)
             _currentPoint.MoveNext();
     }

How can I make the path so my object moves from point 'a, b, c' then loops not just end on point 'c' and gives me the error 'IndexOutOfRangeException: Array index is out of range'

I'm new to coding to an answer would be great! :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by slavo · Aug 28, 2014 at 04:54 PM

You need to check that you are at the end of array. Array are indexed from 0 in c#.

         if (index <= 0)
             direction = 1;
         
         if (index == Points.Length - 1)
         {
             index = 0;
         }
         else
         {
             index = index + direction;
         }

Bit off topic, but use of Corutines to get you next point is Overengineering. They are mostly use if your function need to run in multiple frames.

You can achive same behaviour with this script:

 using UnityEngine;
 using System;
 
 public class FollowPath : MonoBehaviour
 {
     public Transform[] Points;
 
     public enum FollowType
     {
         MoveTowards,
         Lerp
     }
     
     public FollowType Type = FollowType.MoveTowards;
     public float Speed = 1;
     public float MaxDistanceToGoal = .1f;
     
     private int _currentPointIndex;
     private int _direction;
     
     public void Start()
     {
         if (Points.Length > 1 == false)
         {
             Debug.LogError("You must specify at least 2 points for path", gameObject);
             return;
         }            
 
         _direction = 1;
         _currentPointIndex = 0;
         transform.position = Points[0].position;
     }
     
     public void Update()
     {
         if (Points.Length > 1 == false)
             return;
         
         if (Type == FollowType.MoveTowards)
             transform.position = Vector3.MoveTowards(transform.position, Points[_currentPointIndex].position, Time.deltaTime * Speed);
         else if (Type == FollowType.Lerp)
             transform.position = Vector3.Lerp(transform.position, Points[_currentPointIndex].position, Time.deltaTime * Speed);
         
         int nextPointIndex = GetNextPointIndex();
         float distanceToGoal = (Points[nextPointIndex].position - transform.position).sqrMagnitude;
 
         if (distanceToGoal < MaxDistanceToGoal * MaxDistanceToGoal )
         {
             _currentPointIndex = nextPointIndex;
         }
     }
 
     public void OnDrawGizmos()
     {
         if (Points == null || Points.Length < 2)
             return;
         
         for (var i = 1; i < Points.Length; i++)
         {
             Gizmos.DrawLine(Points[i - 1].position, Points[i].position);
         }
     }
 
     private int GetNextPointIndex()
     {
         //check if you are at end of array, go to start if yes
         if (_currentPointIndex == Points.Length - 1)
             return 0;
 
         //else return next point
         return _currentPointIndex + 1;
     }
 }

Its pseudo code, so it can contains some runtime errors.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Making a block move when its clicked 1 Answer

iOS Tap to move?? 2 Answers

Mathf.SmoothStep not quite working. 1 Answer

Change bool in an array from another script 2 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