Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 borispavlov0 · Jun 11, 2015 at 09:00 AM · movementtransformspeedjerkytowards

Jerky motion using moveTowards at high speed

I have two objects having a slightly different maxSpeed parameter and a camera attached to one of them. I am using the following script to move them:

 using UnityEngine;
 using System.Collections;
 
 public class Character : MonoBehaviour {
 
     public float speed = 0f;
     public float maxSpeed = 0f;
     
     protected int pathIndex = 0;
     protected int maxTrackIndex = 0;
     
     protected Vector3 targetDirection;
     protected Vector3 targetPosition;
     protected GameController gameController;
 
         void Start () {
         gameController = GameObject.FindGameObjectWithTag ("Scripts").GetComponent<GameController> ();
         transform.position = gameController.GetCheckpointAtIndex (0);
         targetDirection = gameController.GetCheckpointAtIndex (0);
         targetPosition = transform.position + targetDirection;
         maxTrackIndex = gameController.GetMaxIndexOfTrack ();
         StartCoroutine ("StartMovingMechanic");
     }
 
     protected IEnumerator StartMovingMechanic () {
         while (true) {
             if (Vector3.Distance (transform.position, targetPosition) < 0.1f) {
                 if (pathIndex < maxTrackIndex) {
                     pathIndex ++;
                 } else {
                     pathIndex = 0;
                 }
                 
                 targetDirection = gameController.GetCheckpointAtIndex (pathIndex);
                 targetPosition += targetDirection;
             }
             
             transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.fixedDeltaTime);
             yield return null;
         }
     }
 
     void Update () {
                if (Input.GetKeyDown (KeyCode.UpArrow)) {
             StartCoroutine ("Throttle");
             StopCoroutine ("Decelerate");
         } 
         if (Input.GetKeyUp (KeyCode.UpArrow)) {
             StopCoroutine ("Throttle");
             StartCoroutine ("Decelerate");
         } 
     }
 
         IEnumerator Throttle () {
         while (speed < maxSpeed) {
             speed = Mathf.Lerp (speed, maxSpeed, Time.deltaTime * 1);
             yield return null;
         }
     }
 
     IEnumerator Decelerate () {
         while (speed > 0.05f) {
             speed = Mathf.Lerp (speed, 0, Time.deltaTime * 1);
             yield return null;
         }
     }
 }
 

I am using a list defined in the game controller that provides the checkpoints which is referenced in Start. Everytime the objects reach close to their destination, they get the next checkpoint.

What I observe is that in the game view, the second object with a slightly lower max speed is jerky when the speed gets higher. It's not consistent and to me it looks random. I am really unsure on how to tackle this. In the Scene View, everything looks ok (or I can't tell, because it's fast).

I am really not sure if this is the right way to go. I don't want to use rigid bodies at the moment, so I am using transforms. I tried searching for the issue, but I really can't figure out what to even search for. The jerkyness looks a lot like a frame or two is skipped. This is a very simple Scene with not a lot of objects/scripts.

Any ideas?

Thank you!

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

2 People are following this question.

avatar image avatar image

Related Questions

How to move an object back/forth on a set path? 2 Answers

Pushing rigidbodies around 1 Answer

Why does the player keep pointing in a specific direction after moving? 0 Answers

How to translate a camera forward and back(basically a zoom) according to the distance of two objects? 1 Answer

rotation speed cap in 2D 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