How to move an object after an amount of time?
Hello guys!
I have a problem which cannot be solved by myself, so please help me. I want to make my object move after 10 seconds but I have no idea how to do this. I searched through questions and answers but it didn't really help.
Thank you for helping me! ...unity-noob overhere !
Answer by TheShadyColombian · Nov 11, 2016 at 06:04 PM
First, create two variables, a float which will represent the delay you want (in this case, 10) and the other will be a timer, whose value we will increase every frame.
In the Update() function, add a line similar to this:
yourTimerVariable += Time.deltaTime;
This will add the time, instead of just the number of frames. Read the docs page for Time.deltaTime for more info. It's a really useful variable.
Then below that, add an if statement that verifies if the timer variable is greater than the variable which stores the delay you want, and execute whatever movement code you have inside those brackets.
Let me know if you need more help.
Answer by lindam345 · Nov 13, 2016 at 01:09 PM
At first - thank you very much @xXJuanXx! I tried to implement your suggestions but it didn't work. Either I am too dumb for this or I have an error in reasoning.
Thats my progress so far, I made the objects move before I wanted them to move after 10 seconds, so i have to change it somehow.. :
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
delay += Time.deltaTime;
if (timer > 10)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
}
One of the problems is that my program did not want to accept float 10 . Do I have to assign 10 to float delay in another way?
Thank you for helping me!
Please post further questions and other things as comments ins$$anonymous$$d of answers.
The first problem is that you are increasing the value of the delay function but you are only verifying if the timer function is greater than 10. What you should be doing is increasing the value of timer, and verifying if timer is greater than delay. You should also be assigning 10 to your delay variable during declaration or in your Start() function (or in the editor, if you don't $$anonymous$$d it being a public variable)
Here's your above code with the changes I suggested
// Use this for initialization
void Start() {
//Set the value of 'delay' to 10:
delay = 10;
}
// Update is called once per frame
void Update() {
//Increase the value of 'timer' by deltaTime:
timer += Time.deltaTime;
//Verify if the timer is greater than delay:
if (timer > delay) {
//If so, proceed to translate the object:
float step = speed * Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.position, step);
}
}
It worked! Thank you very much! You were very patient with me. I hope I can ask you another question if I am not able to manage some things by myself.
Greetings!
You're very welcome! I'm glad I was able to help.
If you ever need more help, create your next post with a tag of my username ( @xXJuanXx ) and I'll be notified.
Also, if it helped, make sure to mark my answer as correct. ^_^
Your answer
Follow this Question
Related Questions
How can I compare transform.position now and transform.position after 2 seconds? 2 Answers
few buttondown in fixed time 1 Answer
Suspension of Physics 1 Answer
Get position of object that uses Perlin Noise X distance/time back/offset? 0 Answers
Change scene after event (time or clicking on object) 0 Answers