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 /
  • Help Room /
avatar image
0
Question by Kurtz · Jun 15, 2017 at 01:22 AM · movementlerptransform.positionmovetowardsvector3.distance

Looking for the optimal smooth incremental movement method.

This is an issue that I have found a lot of information on and that part of my trouble. There is so much information that I think I’m losing track of the right solution.

What I am attempting to do is smoothly move the Player object from its current location to a new location in a stepwise manner--each keypress moves a "space" in a 3d maze. After a lot of reading and not a little code borrowing I have come up with a solution that seems to work, but I’ve seen so many approaches to this that I’d really like to know what the preferred or optimal method is. I’m green enough not to trust that I’ve hacked together a good solution.

This is all being done by manipulating the position properties of the object--it's not physics-based.

In outline what I have done is this:

 1. Determine end location
 2. Call the coroutine SmoothMovement to handle that actual translation
 3. SmoothMovement
 -- Store remaining distance as a float
 -- While remaining distance > 0:
 -- ?? Determine an intermediate position using Vector3.MoveTowards ??
 -- Translate to the intermediate position
 -- Compute new remaining distance and store it
 -- Yield return null

The actual code is:

     private bool Move(CellCoordinates destination)
     {
         // Compute movement node location in adjacent cell
         float scaledX = destination.X * LevelManager.scale + (LevelManager.scale / 2);
         float scaledZ = destination.Z * LevelManager.scale + (LevelManager.scale / 2);
 
         StartCoroutine(SmoothMovement(new Vector3(scaledX, cameraHeight, scaledZ)));

         return true;
     }
 
     private IEnumerator SmoothMovement(Vector3 end)
     {
         float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
         isMoving = true;
 
         while (sqrRemainingDistance > float.Epsilon) // Close enough
         {
             // ??? Is this the best way to calculate an intermediate point? What about Lerp? Vector3.Distance? ???
             Vector3 newPosition = Vector3.MoveTowards(transform.position, end, Time.deltaTime * speed);
             // ?? Is this the right way to effect movement on the player (absent physics?) ??
             transform.position = newPosition;
             sqrRemainingDistance = (transform.position - end).sqrMagnitude;
             yield return null;
         }
         transform.position = end; // avoid any drift
         isMoving = false;
     }

There are notes in the comments with some specific questions.

The primary one is how to best calculate the intermediate point in the SmoothMovement method. I've seen several ways to do this and my current code uses Vector3.MoveTowards. Is this the wrong method to use? What is the preferred method and why?

Secondary question is: Is just resetting the transform.position the elegant way to move an object outside physics?

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

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

Object stops lerping when target is far away 0 Answers

Javascript Script for Object Follow? 0 Answers

How to calculate distance traveled given time and speed 0 Answers

How can I translate my game object from a certain coordinate to the negative values of the same coordinate? 1 Answer

How can I use both mouse and keyboard to do the same thing? [SOLVED] 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