- Home /
 
Android 2d movement/waypoint script issues
Hi,
I have a movement script which makes an object move to different manually created waypoints in 2d. I got the script from a website but on android it sometimes gets glitchy and the objects moving move strangely. Does anyone know what I can do to make the movement less glitchy?
 using UnityEngine;
 using System.Collections;
 
 public class EnemyMove : MonoBehaviour {
 
     public Waypoint[] wayPoints;
     public float maxSpeed,minSpeed,speed;
     public bool isCircular;
     // Always true at the beginning because the moving object will always move towards the first waypoint
     public bool inReverse = true;
     
     private Waypoint currentWaypoint;
     private int currentIndex   = 0;
     private bool isWaiting     = false;
     private float speedStorage = 0;
     
     
     
     /**
      * Initialisation
      * 
      */
     void Start () {
         speed = Random.Range (minSpeed, maxSpeed);
         if(wayPoints.Length > 0) {
             currentWaypoint = wayPoints[0];
         }
     }
     
     
     
     /**
      * Update is called once per frame
      * 
      */
     void Update()
     {
         if(currentWaypoint != null && !isWaiting) {
             MoveTowardsWaypoint();
         }
     }
     
     
     
     /**
      * Pause the mover
      * 
      */
     void Pause()
     {
         isWaiting = !isWaiting;
     }
     
     
     
     /**
      * Move the object towards the selected waypoint
      * 
      */
     private void MoveTowardsWaypoint()
     {
         // Get the moving objects current position
         Vector3 currentPosition = this.transform.position;
         
         // Get the target waypoints position
         Vector3 targetPosition = currentWaypoint.transform.position;
         
         // If the moving object isn't that close to the waypoint
         if(Vector3.Distance(currentPosition, targetPosition) > .1f) {
             
             // Get the direction and normalize
             Vector3 directionOfTravel = targetPosition - currentPosition;
             directionOfTravel.Normalize();
             
             //scale the movement on each axis by the directionOfTravel vector components
             this.transform.Translate(
                 directionOfTravel.x * speed * Time.deltaTime,
                 directionOfTravel.y * speed * Time.deltaTime,
                 directionOfTravel.z * speed * Time.deltaTime,
                 Space.World
                 );
         } else {
             
             // If the waypoint has a pause amount then wait a bit
             if(currentWaypoint.waitSeconds > 0) {
                 Pause();
                 Invoke("Pause", currentWaypoint.waitSeconds);
             }
             
             // If the current waypoint has a speed change then change to it
             if(currentWaypoint.speedOut > 0) {
                 speedStorage = speed;
                 //speed = currentWaypoint.speedOut;
             } else if(speedStorage != 0) {
                 speed = speedStorage;
                 speedStorage = 0;
             }
             
             NextWaypoint();
         }
     }
     
     
     
     /**
      * Work out what the next waypoint is going to be
      * 
      */
     private void NextWaypoint()
     {
         if(isCircular) {
             
             if(!inReverse) {
                 currentIndex = (currentIndex+1 >= wayPoints.Length) ? 0 : currentIndex+1;
             } else {
                 currentIndex = (currentIndex == 0) ? wayPoints.Length-1 : currentIndex-1;
             }
             
         } else {
             
             // If at the start or the end then reverse
             if((!inReverse && currentIndex+1 >= wayPoints.Length) || (inReverse && currentIndex == 0)) {
                 inReverse = !inReverse;
             }
             currentIndex = (!inReverse) ? currentIndex+1 : currentIndex-1;
             
         }
         
         currentWaypoint = wayPoints[currentIndex];
     }
 }
 
               and the waypoint scritp attatched to the waypoints:
 using UnityEngine;
 using System.Collections;
 
 public class Waypoint : MonoBehaviour
 {
     public float waitSeconds = 0;
     public float speedOut = 0;
 }
 
               Any help is greatly appreciated, thanks.
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
I Need Help With Mobile Touch Movement. 0 Answers
Android OnMouseDown character movement 2 Answers
Move horizontally on touch 0 Answers
Linear laggy/stuttery movement 1 Answer
Smooth movement between waypoints 2D 1 Answer