#deltaTime
1 messages · Page 1 of 1 (latest)
(made a thread)
If you want to change something over time, you need to know how much to change it in each step.
So if you're just moving an object 1 meter instantly, you do this
transform.position += Vector3.forward;
This immediately moves you forward by 1 meter.
But if you want to move yourself forward one meter over one second, you need to move by a little bit each frame.
I do not understand this, then.
Why does this make you think you aren't going to use deltaTime?
But I dont know how to move object soothly with just one frame
Because I have to repeat same line of code so its the same as I would use like * 0.1 instead of * time.deltaTime
Like I know the value is diffrent in 0.1 and delta
But its pretty much the same functionality
No, they're quite different.
void Update() {
transform.position += Vector3.forward * 0.1f;
}
void Update() {
transform.position += Vector3.forward * Time.deltaTime;
}
The first Update method moves you forward by 0.1 meters every frame.
Dude omg
The second Update method moves you forward by 1 meter per second.
you cannot move something smoothly in one frame.
because you'd have to move it instantly.
Thats what I wasking for
IEnumerator Move()
{
interactObject.position += Vector3.up * 2f * Time.deltaTime;
interactObject.position += Vector3.up * 2f * Time.deltaTime;
interactObject.position += Vector3.up * 2f * Time.deltaTime;
interactObject.position += Vector3.up * 2f * Time.deltaTime;
yield return new WaitForSeconds(2f);
}
So I should do somethling liek this?
why would you expect this to be "smooth"?
it's adding to the position four times
that will instantly move you by whatever 2 * Time.deltaTime * 4 meters happens to be
By calling the method that runs frames inside like IEnumator. U call it once and it counts inside his own time
Coroutines work exactly like any other method, with an important addition
when you yield, the method is suspended until it gets resumed later
then you'll need to set the position many times.
yield return null; will suspend until the next frame
U said it
Yes -- over the course of many frames
exactly like I showed here
You'll do something very similar in your coroutine.
Coroutines just make it easier to write code that needs to do some work over many frames
you don't have to stick it in your Update method and keep all of the variables straight
So how can I call a function that will move an object smoothly?
Like lets say I pull the lever and it has to smoothly move the object from right to left
IEnumerator Move(Vector3 from, Vector3 to, float duration) {
float lerpTime = 0;
while (lerpTime < 1) {
lerpTime = Mathf.MoveTowards(lerpTime, 1, Time.deltaTime / duration);
transform.position = Vector3.Lerp(from, to);
yield return null;
}
}
This is how I would write it.
You tell it where to start from and where to end up.
as well as how long it should take
Vector3.Lerp and Time.DeltaTime
lerpTime goes up each frame. It goes up slowly if the duration is long, and it goes up quickly if the duration is short.
Vector3.Lerp gives you a vector between the start and end vectors.
If the third argument is zero, you get the start. If the third argument is one, you get the end.
If your game is running at 100 frames per second and the duration is 1, lerpTime goes up by 0.01 each frame
So after 100 frames, it's at 1, and the coroutine ends
It is very important to understand why this doesn't work
Yeah I know why its now working
Coroutines are like normal methods, with the exception that you can suspend them with a yield
Thats why I was confused when u told me to do this many times
Mate u are overcomplicating things
I know how that works
I am not begginer
I just never used Lerp so I didnt knew about it
And I asked how can I achive it without time.deltaTime
But u are overcomplicating things
U could just text me "Use Vector3.Lerp"
And I would know what to do
But thanks for help
the mistakes you made tell me that you don't know as much as you think you do.
that is fine.
but i do not appreciate this tone
It wasnt mistakes it was example of what I want to achive and then u give me results of something that for me are not gonna work but I did them anyway to show you that its not working
Vector3 newPosition = Vector3.Lerp(transform.position, targetPosition.position, speed * Time.deltaTime);
```Lerps the object to a new transform position
and ```cs
transform.position = newPosition;
```updates the object's position
That's basic usage of Vector3.Lerp
Thank you
This will produce a speed that varies with how far you are from the destination
it also doesn't work consistently as the framerate changes.
Yes, but I don't know what exactly is required here since i just entered this discussion
If you want to move at a constant speed towards a destination, Vector3.MoveTowards is very helpful.
Exactly
MoveTowards will make the speed constant
Btw @twin citrus , you mentioned something about my particle system and Emit(), I'd like some of your help if you don't mind
I'm new with particles and such
This is how they always are. They say something that proves they don't know the subject, then get upset when someone explains. It's really frustrating, especially when THEY are the ones getting mad for getting helped
I see.