- Home /
create speed booster c#
I'm creating a runner game where the player gets a speed boost after running into a 'boost coin' this is what i have so far, but it's not working. can anyone help me?
public class Booster : Coin {
public float bonusTime;
public float bonusGain = 500f;
private bool picked = false;
private float timer = 20f;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
picked = true;
GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner>().speedRunner += bonusGain;
Destroy(gameObject);
}
}
void Update()
{
if (picked == true)
{
timer += Time.deltaTime;
if (timer >= bonusTime)
{
GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner>().speedRunner -= bonusGain;
Destroy(gameObject);
}
}
}
}
my Player doesn't go faster after running into a boost coin
What's speedRunner
? What script does it belong to? How does that script use it? Why do you destroy the game object? Won't that stop the Update
from happening?
You should probably use a variable to store ControlRunner in.
ControlRunner control;
void Start(){
control = GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner> ();
}
Then you can just use this code in Update and OnTriggerEnter:
control.speedRunner -= bonusGain;
That's all I can say without seeing more of your code.
I don't see you setting the bonusTime variable anywhere.
Also, suggest you replace this:
GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner>().speedRunner += bonusGain;
with this:
other.GetComponent< ControlRunner >().speedRunner += bonusGain;
Answer by sniper43 · Feb 17, 2015 at 01:34 PM
Your ControlRunner might not properly be registering this.
ASSUMING your ControlRunner script is working correctly (you should post it) the collision isn't being detected. This can happen if you are changing the position of the obejcts around you while using a rigibody, but without further info, I won't delve into that.
Also, if you destroy the coin in the triger event, the entire
if (picked == true)
loop will not EVER execute.
this is the code for ControlRunner
public float speedRunner = 2f;
private Vector3 moveDirection = Vector3.zero;
public bool grounded = true;
public float time = 4.0f;
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
{
transform.position += Vector3.left * speedRunner * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
transform.position += Vector3.right * speedRunner * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
{
}
}
void OnCollisionEnter(Collision hit)
{
grounded = true;
// check message upon collition for functionality working of code.
Debug.Log("I am colliding with something");
}
}
agree with sniper: you probably want to countdown/time the speed boost in the player script (ControlRunner), since you destroy the coin.
OR, rather than delete the coin on collisoin, make it invisible, and destory only in update, when done with speed boost timer.
Your answer
Follow this Question
Related Questions
Power Up speed and jump boost 2 Answers
How to increase the speed of an object's movement when it enters a trigger? 1 Answer
Problem with speed boost 2 Answers
Adding Speed to onTriggerEntered 1 Answer
Limited Energy Regeneration 3 Answers