For example using the Mathf.Lerp function. The lerp value is a float between 0 and 1 and determines which ‘in between point’ from start to end should be returned. Vector3.Lerp takes 3 parameters: Vector3 start position, vector3 end position and a float lerp value. Unity has a several lerp functions for various variable types, but this guide is explaining smooth movement so I’ll be focusing on Vector3.Lerp but referencing Mathf.Lerp for explaining some basics. ![]() (A, B, C, D, E… etc) Unity Lerp Functions An example is if you’re wanting to lerp from A to Z instead of jumping directly you could jump to everything in between to make the change feel smoother. Lerping is where you interpolate a value from start to finish, this basically means to fill in the gap between start and finish gradually. This is because as the frame rate is lowered the time since last frame actually increases and visa-versa for lower frame rates. We can use the time since last frame (deltaTime) to instead move the ball by (deltaTime * speed) pixels per frame which will make the ball always travel the same distance over the same amount of time, moving at a consistent speed. If the game is running at 10 frames per second (you can never be sure what frame rate you game will be running at on all systems!) then the ball will move 10 pixels in 1 second, meaning it’s moving 10 times slower! If the frame rate of the game is 100 then the ball will have moved 100 pixels in 1 second. Lets say you have a ball moving across the screen at 1 pixel per frame. how does the deltaTime help me with slower movement? You will want to use deltaTime movement in combination with either lerping or a interpolated rigidbody! (Both explained in their sections below!) But wait. Note: Using only deltaTime alone may still give stutter at lower framerates or at higher movement speeds. Unity has a variable named deltaTime (ltaTime) which gives the time in seconds since the last Update() call as well as fixedDeltaTime (Time.fixedDeltaTime) which is the constant time since the last FixedUpdate() (Which is a consistant value changed in the time manager or directly changed via script, but it’s handy to have) (Tip: Using ltaTime inside a FixedUpdate() will give you the fixedDeltaTime value) Otherwise when the frame rate of the game changes, the speed of the objects will also change! Moving objects based on frame rate is very important for creating smooth movement. ![]() position of a moving object per frame will create a lot of stuttering.Īdapting movement to the framerate with deltatime ![]()
0 Comments
Leave a Reply. |