I'm playing around with projectile physics on objects in Unity. In this case I'm just using a sphere object and calculating it's vector and the path it should take based on gravity (leaving other forces like friction out for the moment).
The formula I'm using is the following: s = ut + ½at²
and the code solution is simple enough as follows:
using UnityEngine;
using System.Collections;
public class TestProjectileForce : MonoBehaviour {
// Create vector components to calculate force & position
private float force = 1.8f;
private float g = 9.8f;
private float angle = 30f;
private float fx;
private float fy;
// projectile calculate
private float dis_x;
private float dis_y;
// Use this for initialization
void Start () {
// calculate x & y vector
fx = Mathf.Round (force * Mathf.Cos(Mathf.Deg2Rad * angle));
fy = Mathf.Round (force * Mathf.Sin(Mathf.Deg2Rad * angle));
}
// Update is called once per frame
void Update () {
// s = ut + 1/2at2
dis_x = (float)(fx * Time.timeSinceLevelLoad);
dis_y = (float)(fy + Time.smoothDeltaTime + (0.5) * (-g) * Time.timeSinceLevelLoad * Time.timeSinceLevelLoad);
Debug.Log (dis_x+", "+dis_y);
transform.Translate (new Vector2(dis_x, dis_y));
}
}
I noticed however, that the expected result i should get varies greatly depending on if i use Time.sinceLevelLoad
over Time.deltaTime
. Can anyone explain to me why this is? When using Time.sinceLevelLoad
i get the expected result of the ball arcing and dropping given the force of gravity acting upon it, however with Time.deltatime
the balls will just shoot off and not arc as expected.
Time.deltaTime is the time interval being processed by this Update. At 30fps it is 1/30s assuming no variance in frame rate. For physics you should use FixedUpdate which is always a fixed interval.
Time.timeSinceLevel load is at it says, the elapsed game time since loading the level.
They are very different concepts. The first can be used to derive an iterative solution, the second to evaluate a function over time (assuming you always want to evaluate from start of level load).