- Home /
How to increase the speed of the character in OnTriggerEnter function?
Hi everyone,
I'm trying to make a game similar to something like Doodle Jump: The character move automatically up, with its speed that progressively decreases.
The issue that I encounter is with the stars, which are power-ups that boost speed for a small amount of time. I tried a lot of different things in the function OnTriggerEnter but it didn't work at all (except adding force to the rigidbody, but consequently I have the game over GUI that appears even if the character still moves, since it thinks that currentSpeed == minSpeed in the Update without taking into consideration the rigidbody.AddForce in the OnTriggerEnter).
There's my code:
// Speed of the Player (left and right movements)
var moveSpeed = 10f;
var movement;
// Decrease-Increase speed (up)
var maxSpeed = 30.0;
var minSpeed = 0.0;
var maxAcceleration = 1.0; // Controls the acceleration
var maxAcceleration2 = 8.0; // Controls the acceleration
var currentSpeed = 30.0;
//var currentSpeed2 = 30.0;
// GUI that appears when Game Over
var showGUI : boolean = false;
var iconGO : Texture2D;
var iconRetry : Texture2D;
var iconBack : Texture2D;
var positionGO : Rect;
var positionRetry : Rect;
var positionBack : Rect;
function Update()
{
// gameplay Player (left and right)
movement = Input.GetAxis("Horizontal") * moveSpeed;
movement *= Time.deltaTime;
transform.Translate(0.0f,0.0f, movement);
// moving automatically up
transform.Translate(Vector3.up * Time.deltaTime * currentSpeed);
// decreasing speed
if (currentSpeed > minSpeed)
{
currentSpeed -= maxAcceleration * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
}
// when the character stops, GUI Game Over
if (currentSpeed == minSpeed)
{
//Debug.Log ("YES"); //working (currentSpeed == 0.0)
showGUI = true;
}
}
function OnTriggerEnter(other: Collider)
{
if (other.tag == "Stars")
{
// Works but the GUI appears before the character stops (when it takes star(s)):
//rigidbody.AddForce (0, 300, 0);
//Debug.Log ("YES 02"); //working (collision)
// one of the many tests that I tried:
transform.Translate(Vector3.up * Time.deltaTime * maxSpeed);
maxSpeed += maxAcceleration2 * Time.deltaTime;
maxSpeed = Mathf.Clamp(maxSpeed, minSpeed, currentSpeed);
// an other one that I tried:
transform.Translate(Vector3.up * Time.deltaTime * currentSpeed2);
currentSpeed2 += maxAcceleration2 * Time.deltaTime;
currentSpeed2 = Mathf.Clamp(currentSpeed2, minSpeed, maxSpeed);
}
}
function OnGUI()
{
if(showGUI)
{
if(GUI.Button(positionGO, iconGO, "label"))
{
// Image Game Over
}
else if(GUI.Button(positionRetry, iconRetry, "label"))
{
// Button that reloads the current scene
Application.LoadLevel("RUNNER");
}
else if(GUI.Button(positionBack, iconBack, "label"))
{
// Button that allows to go back to the menu
Application.LoadLevel("MENU");
}
}
}
Maybe, to resolve it easily, is there a code that simply says "show me the GUI once my character stopped (so, not related to currentSpeed and minSpeed in the Update)" ?
Thanks in advance for all the help I can get !
Answer by Bloodyem · May 31, 2013 at 03:15 PM
Ok, I'm fairly new myself, so I'm kinda guessing, but what I think I would do would make it so when you got a star, it turned on a boolean, and while the boolean was active, currentSpeed = maxSpeed, and then use Time.deltaTime to figure out how long he had been boosted for.
function OnTriggerEnter(other: Collider)
{
if (other.tag == "Stars")
{
var start : boolean = true;
//Right here is where we have the setup. All collecting the star will do is alter the speed you //are moving, and once 5 seconds has gone by, should revert back to normal.
if (star == true)
{
timeCheck = 0;
currentSpeed = 50;
timeCheck += Time.deltaTime;
if (timeCheck >= 5)
{
currentSpeed = 30;
star = false;
}
}
}
transform.Translate(Vector3.up * Time.deltaTime * maxSpeed);
maxSpeed += maxAcceleration2 * Time.deltaTime;
maxSpeed = Mathf.Clamp(maxSpeed, minSpeed, currentSpeed);
Sorry if it's not ideal or perfect, but it ought to work.
Ok hum you don't have to be sorry, it just worked perfectly ;-)
Thank you very much Bloodyem, I feel so dumb to not have thought about something as logical as this..!
-For someone else who could have a similar problem in the future and find this page (even if it's obvious), or if you can edit your answer Bloodyem: the 'var' in 'var timeCheck += Time.deltaTime;' is unnecessary of course ;-)
Thanks again!
Your answer
Follow this Question
Related Questions
Increasing the speed for a few seconds doesn't work. 1 Answer
Smooth increasing and decreasing inside Invoke function? 2 Answers
Increase speed in float over time. 0 Answers
Simple "E" to interact script... 1 Answer
OnTriggerEnter only recognised once, no matter how many times I enter ? (Solved) 1 Answer