'Unity - Time.deltaTime sets m velocity to ~0
New to coding and C# in general, it looks like I cannot understand how to use Time.deltaTime despite reading the documentation.
I am developing a small 2D plateformer and I have now several objects. After noticing some stuttering I read I was supposed to multiply my movement vectors by time.DeltaTime.
When I do so however, my objects stop moving. After some investigation I found out that Time.delaTime almost equals 0 and varies with each frame. So what happens is that I technically multiply my object velocity with a vale close to zero, which in turns results in my object looking immobile.
What is the correct way to use Time.deltaTime in my example?
Edit: adding my code for that object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
Rigidbody2D enemyRigidbody;
int enemyDirection = 1;
[SerializeField] float enemySpeed = 2f;
void Start()
{
enemyRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
EnemyMove();
}
private void EnemyMove()
{
enemyRigidbody.velocity = new Vector2(enemyDirection * enemySpeed * Time.deltaTime, 0f);
Debug.Log(enemyRigidbody.velocity);
Debug.Log(Time.deltaTime);
}
//Collisions turn around
private void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Platforms")
{
transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y);
enemyDirection = enemyDirection * -1;
}
}
}
Solution 1:[1]
In most cases*, including this one, you shouldn't use Time.deltaTime to assign velocity, as then the movement speed on each frame (which, note, is different from movement distance) will change based on the device's framerate.
In other words, when calculating movement distance you do want to take the framerate into account, so you should use deltaTime. But here you are calculating movement velocity, which should not take the framerate into account.
Using deltaTime when assigning velocity in the way that you are would result in situations like having a projectile that is designed to cross the screen in 5 realtime seconds, cross the screen in an unpredictable amount of time based on the performance of the device.
In short, just use enemyDirection * enemySpeed:
enemyRigidbody.velocity = new Vector2(enemyDirection * enemySpeed, 0f);
You can refer to the official Unity documentation for Rigidbody2D.velocity for an example that also does not use deltaTime to assign to velocity:
//Press the Up arrow key to move the RigidBody upwards if (Input.GetKey(KeyCode.UpArrow)) { rb.velocity = new Vector2(0.0f, 2.0f); moving = true; t = 0.0f; } //Press the Down arrow key to move the RigidBody downwards if (Input.GetKey(KeyCode.DownArrow)) { rb.velocity = new Vector2(0.0f, -1.0f); moving = true; t = 0.0f; }
*A case where you would want to include deltaTime in velocity calculations is if you have a value that represents acceleration (note, not velocity or distance) and and you want to calculate how much velocity should increase or decrease from the velocity of the last frame.
Solution 2:[2]
I am still very new to Programing as well so take what I say with a grain of salt. I seem to remember reading that since Time.deltaTime has to do with making your movement framerate independent, I think the answer could be to put your EnemyMove(); call in a Fixed Update function. Again I am probably less experienced than you this just came to mind as I was reading your question. Hek I,m new to Stackoverflow.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | user18307591 |
