Hello again everybody,
so I am working on a FPS tower defense game and of course there are bullets to move. I do that using the following code in a script attached to the bullet:
GetComponent<Rigidbody>().velocity = direction * Time.deltaTime * bulletSpeed;
with a bulletspeed of 10k which I set up on a low fps laptop and the speed seemed just fine.
Back on my main PC with way higher fps the bullet is extremely slow.
Now I am still pretty new to Unity and C# scripting in general but in pretty much every tutorial I watched they said using Time.deltaTime would fix that exact problem. Is there another way to fix it? My limited knowledge doesn’t allow me to come up with a solution myself.
You shouldn’t set velocity, you should change position. On your better PC fps is higher, i.e Time.deltaTime is lower, of course result velocity will be very low. It should be:
For more consistency you can use FixedUpdate, since even with Time.deltaTime it won’t be 100% accurate, to make everything smooth you can change projectile rigidbody to interpolation.
private void Awake()
{
rigidbody.interpolation = RigidbodyInterpolation.Interpolate
}
private void FixedUpdate()
{
//deltaTime will be automatically fixedDeltaTime
rigidbody.position += direction * (Time.deltaTime * speed);
}
oh wait did I just mix up position and velocity? Is velocity independent from fps and setting velocity without Time.deltaTime would archieve the same speed on different devices? Because I really liked just setting the velocity once instead of moving the bullet each frame
If projectile isn’t stopped by anything and have constant speed, then you can simply set velocity one time in Awake/Start without Time.deltaTime, and yes, this will work.