- Home /
C# Make something happen for X amount of Seconds.
Hello, the title says almost all there is. I would like to know how i would do something like this in c#. if(input.GetKeyDown("2")){
spin();
}
spin()
{
//how to make this happen for 5 seconds, even after the key is //up?
transform.rotation.z - 1;
//and then how do i make it return to original rotation?
}
Regards Brim
I think this answer will help you. You should look into using coroutines.
Answer by Peter G · Jul 31, 2013 at 04:27 PM
You should probably use a coroutine:
class RotateClass : MonoBehaviour{
public float turnSpeed = 90f;
//turn speed in degrees.
public float turnTime = 5f;
//how long should we turn for.
private bool spinning = false;
//are we currently spinning?
//we use this to prevent multiple instance of spinning starting.
void Update(){
if (input.GetKeyDown("2") && !spinning)
StartCoroutine( Spin() );
}
IEnumerator Spin () {
spinning = true;
var time = 0;
var turnIncrement = new Vector3( 0 , 0 , turnSpeed * Time.deltaTime);
//Turn towards the side.
while ( time < turnTime ) {
time += Time.deltaTime;
transform.rotation.eulerAngles += turnIncrement;
yield return null;
}
//Turn back to the starting position.
while ( time > 0 ) {
time -= Time.deltaTime;
transform.rotation.eulerAngles -= turnIncrement;
yield return null;
}
spinning = false;
}
}
Yield co-routines become a headache. They make the code very complex even for the simple solutions. I don't use them and I don't suggest the beginners to use them.
The fact you don't use them does't mean they aren't the right way to do things. Doing it on update requires every such time delayed function to have a flag and a separate function. Coroutines are definitely the right answer
Storing the keydown time and testing it against the current time is way simpler to write and to be understood by anyone else. Just look at the code length of your code and $$anonymous$$e.
Right. It works fine if it only needs to spin. What if you want it to spin when you press one key, and jump when you press the next, but want it simultaneously. Checking on Update is a fine way to do it if you never want to extend it.
Also the length of code could be made exactly the same, @Peter G just polished the rotation code.
Oh sorry, I thought PeterG was commenting with me. Implementing different behaviors for different keys is not the topic of this question. And I'm sure that behavior is also possible by storing the event times ins$$anonymous$$d of using yield methods. I won't think about it since it's not the current question.
Answer by Xtro · Jul 31, 2013 at 02:44 PM
1) Store the time when key down
2) Don't put the Spin call in the keydown check (if)
3) Before spin call, test the current time against stored key down time + 5
4) if you pass the test, run your work.
Should be something like that:
class YourClass : MonoBehaviour{
float KeyDownTime = float.MinValue;
Quaternion OriginalRotation;
void Start(){
OriginalRotation = transform.rotation;
}
void Update(){
if (input.GetKeyDown("2")) KeyDownTime = Time.time;
if (Time.time < KeyDownTime + 5){
Spin();
}
else transform.rotation = OriginalRotation;
}
void Spin(){
var R = transform.rotation;
R.z--;
transform.rotation = R;
// in javascript we could do only 1 line...
// transform.rotation.z--;
// instead of 3 lines of C# code
// but in C# we have to use a temp R value.
}
}
this just gives me error at the "transform.rotation.z - 1;" That "Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
Also, when i remove it and just test it with debug.log it bugs out when i start the game :S
I just copied your line into Spin method. You should do the syntax fixing according to your logic.
What bug do you see when you run it ? (I didn't test the code on my unity. I just wrote a template for you to understand)
This line is totally wrong:
transform.rotation.z - 1;
It's an expression not a statement. That's what's giving him the error.
You can't assign a value like that in C#. You have to copy the entire quaternion to a temporary variable, change the value that you want, then reassign it to
transform.rotation
.I think the OP means to edit the euler angles.
Yes, I know where the error is. Like I said I just copied his line of code assu$$anonymous$$g he can fix that line. Anyway, I'ma edit my answer for a better testing.
The only thing with this is the item will spin faster or slower due to the machine its being run on wont it? Since you aren't using Time.deltaTime to standardize the rotation? I would personally go with a IEnumerator, the concept might be a little hard to grasp if you're new to coding but once figured out they are incredibly helpful. This seems like a good simple practice situation to learn how to use them.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Find time value between two other time values? C# 2 Answers
How to double jump 1 Answer
Pause game while GUI instructions displayed? 1 Answer
Pause Game When Function Is Enabled? 3 Answers