- Home /
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!
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.
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.
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; }
}
}
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
Follow this Question
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