Quantcast
Channel: Questions in topic: "independent"
Viewing all articles
Browse latest Browse all 102

Yet another Time.deltaTime question

$
0
0
Jumping and framerate independence. Tried solutions, didn't like. Here is simple example that demonstrates the point - empty scene, attached this to gameobject: using UnityEngine; using System.Collections; public class jumpTest : MonoBehaviour { private float velocityY = 0; private int counter = 0; private float jumpHeight = 0.1f; private Vector3 gravity = new Vector3(0,-.1f,0); // Update is called once per frame void Update () { // gravity framerate independent float gravityY = gravity.y * Time.deltaTime; if (counter == 0) { // Jump is impulse, wont mult jumpHeight // @see http://answers.unity3d.com/questions/1156824/forcemode2dimpulse-do-we-need-timedeltatime.html // velocity framerate independent velocityY += (jumpHeight + gravityY); } else { velocityY += gravityY; } if (velocityY < 0) { velocityY = 0; } transform.position = new Vector3 ( transform.position.x, transform.position.y + velocityY, transform.position.z ); counter++; } } So what do we have here - first frame execution - add "upward impulse" to gameObject. future frames: applies gravity to the gameobject, so that at some point going up, the gravity takes over and gameObject starts falling down. As soon as we register falling down, we STOP! the simulation and check the Y position. I expect if this was framerate-independent we would get approximately the same Y position from multiple runs (difference in +/- size of last deltaTime as the update ticks differ from run to run), However, this aint the case. The y-position differs wildly from run to run. Also on mobiles where framerate dips lower, Y-position is higher. Framerate-dependent. How do we make this piece of code FPS independent?

Viewing all articles
Browse latest Browse all 102

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>