#deltaTime

1 messages · Page 1 of 1 (latest)

twin citrus
#

(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.

devout violet
#

I know mate

#

How time.deltaTime works

twin citrus
#

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?

devout violet
#

But I dont know how to move object soothly with just one frame

devout violet
#

Like I know the value is diffrent in 0.1 and delta

#

But its pretty much the same functionality

twin citrus
#

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.

devout violet
#

Dude omg

twin citrus
#

The second Update method moves you forward by 1 meter per second.

devout violet
#

I know

#

But its in Update

twin citrus
#

because you'd have to move it instantly.

devout violet
#

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?

twin citrus
#

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

devout violet
twin citrus
#

Coroutines work exactly like any other method, with an important addition

#

when you yield, the method is suspended until it gets resumed later

devout violet
twin citrus
#

yield return null; will suspend until the next frame

devout violet
#

U said it

twin citrus
#

Yes -- over the course of many frames

twin citrus
#

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

devout violet
#

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

twin citrus
#
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

shy wraith
twin citrus
#

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

twin citrus
devout violet
#

Yeah I know why its now working

twin citrus
#

Coroutines are like normal methods, with the exception that you can suspend them with a yield

devout violet
#

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

twin citrus
#

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

devout violet
#

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

shy wraith
#
 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

devout violet
#

Thank you

twin citrus
#

it also doesn't work consistently as the framerate changes.

shy wraith
twin citrus
#

If you want to move at a constant speed towards a destination, Vector3.MoveTowards is very helpful.

shy wraith
#

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

wary night
# twin citrus but i do not appreciate this tone

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

twin citrus
#

I see.