- Home /
Increasing the speed for a few seconds doesn't work.
Hey guys.
If my player enter the trigger the speed should increase to 15 and after 10 seconds to 10 but with my script the speed goes to 10 instead of 15.
Here is the script:
using UnityEngine;
using System.Collections;
public class SpeedPickup : MonoBehaviour {
public string tag = "Player";
public PlayerControllerScript controller;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == tag)
{
controller.maxSpeed = 15f;
Destroy(gameObject);
speedTime();
revertSpeed();
}
}
IEnumerator speedTime ()
{
yield return new WaitForSeconds(10);
}
void revertSpeed ()
{
controller.maxSpeed = 10f;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == tag)
{
controller.maxSpeed = 15f;
StartCoroutine("speedTime");
Destroy(gameObject); // other.gameObject no?
}
}
IEnumerator speedTime ()
{
yield return new WaitForSeconds(10);
revertSpeed();
}
void revertSpeed ()
{
controller.maxSpeed = 10f;
}
Answer by Crazydadz · Mar 27, 2014 at 12:18 AM
If you destroy your gameObject before the end of the coroutine, the coroutine will stop. Put Destroy(gameObject) after the coroutine.
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == tag)
{
controller.maxSpeed = 15f;
StartCoroutine(speedTime());
}
}
IEnumerator speedTime ()
{
yield return new WaitForSeconds(10);
revertSpeed();
Destroy(gameObject);
}
void revertSpeed ()
{
controller.maxSpeed = 10f;
}
Thanks that works.
I have made it so. Will I get performance issues(for mobile device) with "GetComponent().enabled = false;"? Or is there a better way?
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == tag)
{
controller.maxSpeed = 15f;
GetComponent<$$anonymous$$eshRenderer>().enabled = false;
StartCoroutine(speedTime());
}
}
GetComponent is quite heavy on $$anonymous$$obile. The best way is to cache it.
private $$anonymous$$eshRenderer _mRenderer;
private void Start ()
{
_mRenderer = GetComponent<$$anonymous$$eshRenderer> ();
}
private void OnTriggerEnter2D (Collider2D col)
{
_mRenderer.enabled = false;
}
Thanks you. :)
As last, if I try to acces another script with "gameObject.GetComponent().walkSpeed = 0.0f;". Is there also a better way for that?
Do the same thing, cache the component on start. If the component is $$anonymous$$yOtherScript, just create a private field of it, allocated it in the Start(), use it in the OnTriggerEnter2D(...)
In your case, you Destroy your gameObject and it is quite heavy on mobile. There is no need of caching component if you destroy your gameObject because when you recreate one, you ll need recall GetComponent....On mobile, try to disable gameObject and not destroy them. I don't know what you want to achieve with that script, but if there is multiple "Speed Trigger", search for "Object Pooling" on the web ins$$anonymous$$d of Destroying each trigger and Instantiating them again.
Your answer
Follow this Question
Related Questions
How to increase the speed of the character in OnTriggerEnter function? 1 Answer
Increase speed of object that enters OnTriggerEnter 1 Answer
Increase forward speed 2 Answers
Increase ball speed by time 1 Answer
fly speed , increasing speed 1 Answer