Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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 emicenizo · Apr 20 at 05:56 PM · movementvector3

Moving an object towards consistently in time, within a number of of "ticks"

So I'm building a game where you can control the speed. Because of use case reasons, I just cannot change the global time scaling on the game, so I'm working with a series of "ticks" (Things that move do so on each ticks). Most movable objects are subscribed to an event that "ticks".

I need to move an object from point A to B in a number of ticks that I have set beforehand - lets say 30. So basically I need to move that object a 1/30th of the way on each tick.

I could simply get the tick number, get a ratio of the distance (named p) and multiply the vector that way. So on each tick my object will move towards that B point:

 Vector3 targetPosition = new Vector3(targetButtonPosition.x * p, targetButtonPosition.y * p, targetButtonPosition.z * p);

Of course, its "teleporting" on each tick. Is there any way to get a more adequate smooth movement with these requirements?

I'm thinking using Lerp should do the trick, but I'm not sure how.

Thanks peeps!

Comment
Add comment · Show 1
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
avatar image Bonfire-Boy · Apr 22 at 02:28 PM 0
Share

Your ticks idea seems overly complicated to me.
To get smooth movement you want to use Time.deltaTime somewhere when doing a frame update. Like in

 Vector3 newPosition = Vector3.MoveTowards( currentPosition, targetPosition, Time.deltaTime * movementSpeed );

And in that final parameter to MoveTowards, you could add another multiplier (which could be globally accessible), that would then effectively be your own timescale.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by homer_3 · Apr 22 at 02:57 PM

Because of use case reasons, I just cannot change the global time scaling on the game,

For this, I usually make my own Time class and set the execution order to be negative. Then in the update, store Time.deltaTime to a member and also have your own timeScale member. Have a GetDeltaTime() function that returns the member deltaTime * the member timeScale. Any object you want to control the time speed of, have it reference this wrapper time class. So you can say

 MyTime.timeScale = .5f
 transform.position += MyTime.GetDeltaTime() * moveSpeed;

Lerp is doing the exact same thing you described when computing p.

Comment
Add comment · Show 2 · 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
avatar image Bonfire-Boy · Apr 22 at 03:19 PM 0
Share

I'm curious, why do you need to do the Script Execution Order trick (and cache the value of Time.deltaTime)? Why can't you just return Time.deltaTime * MyTime.timeScale?
Which then effectively becomes the same as the solution I mentioned in my comment, but with a function to wrap up that calculation... which I agree is an improvement. What puzzles me is why this even needs to be a MonoBehaviour. Can't it be a simple static class like this?

 public static class MyTime
 {
    public float myTimeScale = 0.5f;
    public float myDeltaTime
    {
         get { return Time.deltaTime * myTimescale; }
    }
 }


avatar image homer_3 Bonfire-Boy · Apr 22 at 03:26 PM 0
Share

That's true, you probably don't really need to keep a copy of deltaTime and do the execution order. I do it just so the class has more control of everything, but your solution is even simpler and should work just as well.

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

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

I am unable to update MoveTowards target during runtime. Object move only to first target. 0 Answers

Move Forward Locally While Still having Collision 1 Answer

What is difference between local and global coord Vector3. 1 Answer

Limit object position which is moved by accelerometer 0 Answers

Smooth motion physics Playmaker 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