Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 12boulla · Feb 18, 2016 at 01:17 PM · android2dmovementwaypointsglitching

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

0 Replies

· Add your reply
  • Sort: 

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

71 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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