- Home /
Change int value for 30 sec...
I've got AI script that moves forward at a set speed. I would like to have the AI to move forward at a faster speed for 30 sec when clicked by the mouse. How would I go about that? I would like to maintain this new speed for 30 seconds.
My Code:
var speed = 6;
var deer = 10;
var tap = 100;
var rotate;
var target : Transform;
var other : Transform;
function OnMouseDown () {
LevelController.Points += tap + 100;
ChangeDirection();
}
Answer by Julien-Lynge · Oct 31, 2011 at 09:02 PM
PuzzleBox,
I'm not going to read through the script you cut-and-pasted whole, so here's the pieces you'll need:
http://unity3d.com/support/documentation/ScriptReference/Input.GetMouseButtonDown.html
http://unity3d.com/support/documentation/ScriptReference/Coroutine.html
http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html
Then:
var speedMultiplier = 1;
if (Input.GetMouse...) speedMultiplier = 2;
yield WaitForSeconds (30);
You can figure out the rest.
I tried adding "WaitForSeconds (30)" but it doesn't effect anything.
You need to read the documentation that I included, which has examples of how to use the code examples I listed.
function On$$anonymous$$ouseDown () { LevelController.Points += tap + 100; speed += speed + 25; ChangeDirection(); yield WaitForSeconds (30); speed = 6; }
Here is what I found out from the Script Reference link you gave, still doesn't work.
On$$anonymous$$ouseDown is not a coroutine. You need to start a new function that is a coroutine and have the action in that.. i.e.
function On$$anonymous$$ouseDown()
{
StartCoroutine(SpeedUp());
}
function SpeedUp()
{
LevelController.Points += tap + 100;
speed += speed + 25;
ChangeDirection();
yield WaitForSeconds (30);
speed = 6;
}